├── .gitignore ├── .idea ├── .name ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── misc.xml ├── modules.xml └── runConfigurations.xml ├── app ├── .gitignore ├── build.gradle ├── libs │ └── libmedia-3.0.jar ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── example │ │ └── akash │ │ └── videoencrptionexa │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── akash │ │ │ └── videoencrptionexa │ │ │ ├── Encrypter.java │ │ │ ├── MainActivity.java │ │ │ ├── VideoDialogFragment.java │ │ │ └── VideoRecyclerAdapter.java │ └── res │ │ ├── layout │ │ ├── activity_main.xml │ │ ├── video_dialog_fragment.xml │ │ └── video_recycler_items.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── example │ └── akash │ └── videoencrptionexa │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── projectFilesBackup └── .idea │ └── workspace.xml └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | VideoEncrptionExa -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.3" 6 | 7 | defaultConfig { 8 | applicationId "com.example.akash.videoencrptionexa" 9 | minSdkVersion 15 10 | targetSdkVersion 23 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | testCompile 'junit:junit:4.12' 25 | compile 'com.android.support:appcompat-v7:23.3.0' 26 | compile 'com.jakewharton:butterknife:5.1.1' 27 | compile 'com.jakewharton:butterknife:5.1.1' 28 | compile 'cz.msebera.android:httpclient:4.4.1.2' 29 | compile 'com.android.support:design:23.3.0' 30 | compile 'com.jakewharton:butterknife:5.1.1' 31 | } 32 | -------------------------------------------------------------------------------- /app/libs/libmedia-3.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akash-bisariya/Video-Encryption-Decryption/72e747f95f44babe40387c52db998dfb03ba2c45/app/libs/libmedia-3.0.jar -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /home/akash/Android/Sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/example/akash/videoencrptionexa/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.example.akash.videoencrptionexa; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/akash/videoencrptionexa/Encrypter.java: -------------------------------------------------------------------------------- 1 | package com.example.akash.videoencrptionexa; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.io.OutputStream; 6 | import java.security.InvalidAlgorithmParameterException; 7 | import java.security.InvalidKeyException; 8 | import java.security.NoSuchAlgorithmException; 9 | import java.security.spec.AlgorithmParameterSpec; 10 | 11 | import javax.crypto.Cipher; 12 | import javax.crypto.CipherOutputStream; 13 | import javax.crypto.NoSuchPaddingException; 14 | import javax.crypto.SecretKey; 15 | 16 | import fr.maxcom.http.LocalSingleHttpServer; 17 | 18 | /** 19 | * Created by akash on 21/5/16. 20 | */ 21 | 22 | public class Encrypter { 23 | private final static int DEFAULT_READ_WRITE_BLOCK_BUFFER_SIZE = 1024; 24 | private final static String ALGORITHM_VIDEO_ENCRYPTOR = "AES/CBC/PKCS5Padding"; 25 | 26 | @SuppressWarnings("resource") 27 | public static void encrypt(SecretKey key, AlgorithmParameterSpec paramSpec, InputStream in, OutputStream out) 28 | throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, 29 | InvalidAlgorithmParameterException, IOException { 30 | try { 31 | Cipher c = Cipher.getInstance(ALGORITHM_VIDEO_ENCRYPTOR); 32 | c.init(Cipher.ENCRYPT_MODE, key, paramSpec); 33 | out = new CipherOutputStream(out, c); 34 | int count; 35 | byte[] buffer = new byte[DEFAULT_READ_WRITE_BLOCK_BUFFER_SIZE]; 36 | while ((count = in.read(buffer)) >= 0) { 37 | out.write(buffer, 0, count); 38 | } 39 | } finally { 40 | out.close(); 41 | } 42 | } 43 | @SuppressWarnings("resource") 44 | public static String decrypt(SecretKey key, AlgorithmParameterSpec paramSpec, InputStream in,String encrypted_path ) 45 | throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, 46 | InvalidAlgorithmParameterException, IOException { 47 | LocalSingleHttpServer mserver; 48 | String path; 49 | mserver= new LocalSingleHttpServer(); 50 | try { 51 | Cipher c = Cipher.getInstance(ALGORITHM_VIDEO_ENCRYPTOR); 52 | c.init(Cipher.DECRYPT_MODE, key, paramSpec); 53 | // out = new CipherOutputStream(out, c); 54 | int count; 55 | 56 | mserver.setCipher(c); 57 | mserver.start(); 58 | path=mserver.getURL(encrypted_path); 59 | 60 | // byte[] buffer = new byte[DEFAULT_READ_WRITE_BLOCK_BUFFER_SIZE]; 61 | // while ((count = in.read(buffer)) >= 0) { 62 | // out.write(buffer, 0, count); 63 | // } 64 | } 65 | finally { 66 | // out.close(); 67 | } 68 | 69 | 70 | 71 | 72 | return path; 73 | } 74 | 75 | } -------------------------------------------------------------------------------- /app/src/main/java/com/example/akash/videoencrptionexa/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.akash.videoencrptionexa; 2 | 3 | import android.app.DialogFragment; 4 | import android.content.Intent; 5 | import android.database.Cursor; 6 | import android.net.Uri; 7 | import android.os.AsyncTask; 8 | import android.os.Build; 9 | import android.os.Environment; 10 | import android.provider.MediaStore; 11 | import android.support.v7.app.AppCompatActivity; 12 | import android.os.Bundle; 13 | import android.util.Log; 14 | import android.view.View; 15 | import android.widget.Button; 16 | import android.widget.TextView; 17 | import android.widget.Toast; 18 | import android.widget.VideoView; 19 | 20 | import java.io.BufferedInputStream; 21 | import java.io.BufferedReader; 22 | import java.io.ByteArrayInputStream; 23 | import java.io.ByteArrayOutputStream; 24 | import java.io.File; 25 | import java.io.FileInputStream; 26 | import java.io.FileOutputStream; 27 | import java.io.IOException; 28 | import java.io.InputStream; 29 | import java.io.InputStreamReader; 30 | import java.io.OutputStream; 31 | import java.net.ServerSocket; 32 | import java.net.URL; 33 | import java.net.URLConnection; 34 | import java.security.InvalidAlgorithmParameterException; 35 | import java.security.InvalidKeyException; 36 | import java.security.NoSuchAlgorithmException; 37 | import java.security.SecureRandom; 38 | import java.security.spec.AlgorithmParameterSpec; 39 | import java.util.ArrayList; 40 | import java.util.HashMap; 41 | 42 | import javax.crypto.KeyGenerator; 43 | import javax.crypto.NoSuchPaddingException; 44 | import javax.crypto.SecretKey; 45 | import javax.crypto.spec.IvParameterSpec; 46 | import javax.crypto.spec.SecretKeySpec; 47 | 48 | import butterknife.ButterKnife; 49 | import butterknife.InjectView; 50 | import fr.maxcom.http.LocalSingleHttpServer; 51 | import fr.maxcom.libmedia.Licensing; 52 | 53 | public class MainActivity extends AppCompatActivity implements VideoDialogFragment.DialogFragmentToActivity { 54 | private final static String ALGO_RANDOM_NUM_GENERATOR = "SHA1PRNG"; 55 | private final static String ALGO_SECRET_KEY_GENERATOR = "AES"; 56 | private final static int IV_LENGTH = 16; 57 | private Cursor mVideoCursor; 58 | private ArrayList> listOfVideo; 59 | @InjectView(R.id.btnDecrypt) 60 | Button btnDecrypt; 61 | @InjectView(R.id.btnEncrypt) 62 | Button btnEncrypt; 63 | @InjectView(R.id.tvText) 64 | TextView tvText; 65 | @InjectView(R.id.vdVideoView) 66 | VideoView vdVideoView; 67 | @InjectView(R.id.btnSelectVideo) 68 | Button btnSelectVideo; 69 | private File inFile; 70 | private File outFile; 71 | private File urlOutFile; 72 | private File outFile_dec; 73 | private File urlOutFile_dec; 74 | private SecretKey key; 75 | private byte[] keyData; 76 | private SecretKey keyFromKeydata; 77 | private AlgorithmParameterSpec paramSpec; 78 | private byte[] iv; 79 | private String path; 80 | private String encrypted_path; 81 | private String selectedVideoPath; 82 | 83 | @Override 84 | public void onActivityResult(int requestCode, int resultCode, Intent data) { 85 | 86 | Toast.makeText(MainActivity.this,"result got activity"+requestCode+resultCode,Toast.LENGTH_SHORT).show(); 87 | } 88 | 89 | 90 | 91 | @Override 92 | protected void onCreate(Bundle savedInstanceState) { 93 | super.onCreate(savedInstanceState); 94 | setContentView(R.layout.activity_main); 95 | ButterKnife.inject(this); 96 | listOfVideo = new ArrayList(); 97 | 98 | final String[] videoColumns = {MediaStore.Video.Media.DATA, MediaStore.Video.Media.DURATION, MediaStore.Video.Media.SIZE, MediaStore.Video.Media.DISPLAY_NAME}; 99 | mVideoCursor = getApplicationContext().getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, videoColumns, null, null, null); 100 | mVideoCursor.moveToFirst(); 101 | for (int i = 0; i < mVideoCursor.getCount(); i++) { 102 | listOfVideo.add(new HashMap() { 103 | { 104 | put("data", String.valueOf(mVideoCursor.getString(mVideoCursor.getColumnIndex(MediaStore.Video.Media.DATA)))); 105 | put("duration", String.valueOf(mVideoCursor.getString(mVideoCursor.getColumnIndex(MediaStore.Video.Media.DURATION)))); 106 | put("displayName", String.valueOf(mVideoCursor.getString(mVideoCursor.getColumnIndex(MediaStore.Video.Media.DISPLAY_NAME)))); 107 | put("size", String.valueOf(mVideoCursor.getString(mVideoCursor.getColumnIndex(MediaStore.Video.Media.SIZE)))); 108 | mVideoCursor.moveToNext(); 109 | 110 | } 111 | }); 112 | } 113 | mVideoCursor.close(); 114 | 115 | Licensing.allow(getApplicationContext()); 116 | 117 | //first or any particular video from sdcard 118 | // tvText.setText(""+listOfVideo.get(0).get("data")); 119 | //path of video saved in sdcard 120 | path = listOfVideo.get(0).get("data"); 121 | 122 | 123 | //playing the video to be encrypted 124 | // vdVideoView.setVideoPath(path); 125 | // vdVideoView.start(); 126 | 127 | 128 | // inFile = new File(listOfVideo.get(0).get("data")); 129 | // outFile = new File(path.substring(0, path.lastIndexOf("/"))+"/encrypted_video.swf"); 130 | // encrypted_path=path.substring(0, path.lastIndexOf("/"))+"/encrypted_video.swf"; 131 | // urlOutFile = new File(path.substring(0, path.lastIndexOf("/"))+"/url_encrypted_video.swf"); 132 | // // outFile_dec = new File(path.substring(0, path.lastIndexOf("/"))+"/decrypted_video.mp4"); 133 | // urlOutFile_dec = new File(path.substring(0, path.lastIndexOf("/"))+"/url_decrypted_video.mp4"); 134 | 135 | try { 136 | key = KeyGenerator.getInstance(ALGO_SECRET_KEY_GENERATOR).generateKey(); 137 | 138 | 139 | keyData = key.getEncoded(); 140 | keyFromKeydata = new SecretKeySpec(keyData, 0, keyData.length, ALGO_SECRET_KEY_GENERATOR); //if you want to store key bytes to db so its just how to //recreate back key from bytes array 141 | iv = new byte[IV_LENGTH]; 142 | 143 | SecureRandom.getInstance(ALGO_RANDOM_NUM_GENERATOR).nextBytes(iv); 144 | } catch (NoSuchAlgorithmException e) { 145 | e.printStackTrace(); 146 | } 147 | 148 | paramSpec = new IvParameterSpec(iv); 149 | 150 | 151 | btnEncrypt.setOnClickListener(new View.OnClickListener() { 152 | @Override 153 | public void onClick(View view) { 154 | inFile = new File(selectedVideoPath); 155 | outFile = new File(selectedVideoPath.substring(0, selectedVideoPath.lastIndexOf("/")) + "/encrypted_video.swf"); 156 | encrypted_path = selectedVideoPath.substring(0, selectedVideoPath.lastIndexOf("/")) + "/encrypted_video.swf"; 157 | // urlOutFile = new File(selectedVideoPath.substring(0, selectedVideoPath.lastIndexOf("/"))+"/url_encrypted_video.swf"); 158 | outFile_dec = new File(selectedVideoPath.substring(0, selectedVideoPath.lastIndexOf("/")) + "/decrypted_video.mp4"); 159 | // urlOutFile_dec = new File(selectedVideoPath.substring(0, selectedVideoPath.lastIndexOf("/"))+"/url_decrypted_video.mp4"); 160 | 161 | 162 | new AsyncTask() { 163 | 164 | @Override 165 | protected void onPostExecute(Void aVoid) { 166 | super.onPostExecute(aVoid); 167 | Toast.makeText(MainActivity.this, "" + selectedVideoPath + " Encrypted Successfully!!", Toast.LENGTH_SHORT).show(); 168 | boolean deleted = inFile.delete(); 169 | if (deleted) { 170 | Toast.makeText(MainActivity.this, "" + selectedVideoPath + " deleted Successfully!!", Toast.LENGTH_SHORT).show(); 171 | 172 | } 173 | refreshSDCard(); 174 | //playing the video we have encrypted 175 | vdVideoView.stopPlayback(); 176 | vdVideoView.setVideoPath(selectedVideoPath.substring(0, selectedVideoPath.lastIndexOf("/")) + "/encrypted_video.swf"); 177 | 178 | vdVideoView.start(); 179 | 180 | 181 | } 182 | 183 | @Override 184 | protected Void doInBackground(Void... voids) { 185 | try { 186 | //encrypting the video from sdcard 187 | Encrypter.encrypt(key, paramSpec, new FileInputStream(inFile), new FileOutputStream(outFile)); 188 | 189 | } catch (IOException e) { 190 | e.printStackTrace(); 191 | } catch (NoSuchPaddingException e) { 192 | e.printStackTrace(); 193 | } catch (InvalidAlgorithmParameterException e) { 194 | e.printStackTrace(); 195 | } catch (NoSuchAlgorithmException e) { 196 | e.printStackTrace(); 197 | } catch (InvalidKeyException e) { 198 | e.printStackTrace(); 199 | } 200 | 201 | return null; 202 | } 203 | }.execute(); 204 | } 205 | }); 206 | 207 | 208 | btnSelectVideo.setOnClickListener(new View.OnClickListener() { 209 | @Override 210 | public void onClick(View view) { 211 | VideoDialogFragment videoDialogFragment = VideoDialogFragment.newInstance(listOfVideo); 212 | videoDialogFragment.show(getFragmentManager(), ""); 213 | } 214 | }); 215 | 216 | 217 | btnDecrypt.setOnClickListener(new View.OnClickListener() { 218 | @Override 219 | public void onClick(View view) { 220 | 221 | ArrayList> decryptedList= searchFile(Environment.getExternalStorageDirectory()); 222 | 223 | VideoDialogFragment videoDialogFragment = VideoDialogFragment.newInstance(decryptedList); 224 | videoDialogFragment.show(getFragmentManager(), ""); 225 | // new AsyncTask() 226 | // 227 | // { 228 | // 229 | // @Override 230 | // protected String doInBackground(Void... voids) { 231 | // String decryptpath=null; 232 | // try { 233 | // 234 | // //decrypting the video saved in sdcard 235 | // decryptpath = Encrypter.decrypt(keyFromKeydata, paramSpec, new FileInputStream(outFile), encrypted_path); 236 | // 237 | // 238 | //// //decrypting the streaming video file saved from url 239 | //// Encrypter.decrypt(keyFromKeydata, paramSpec, new FileInputStream(urlOutFile), new FileOutputStream(urlOutFile_dec)); 240 | // 241 | // } catch (NoSuchAlgorithmException e) { 242 | // e.printStackTrace(); 243 | // } catch (NoSuchPaddingException e) { 244 | // e.printStackTrace(); 245 | // } catch (InvalidKeyException e) { 246 | // e.printStackTrace(); 247 | // } catch (InvalidAlgorithmParameterException e) { 248 | // e.printStackTrace(); 249 | // } catch (IOException e) { 250 | // e.printStackTrace(); 251 | // } catch (Exception e) { 252 | // e.printStackTrace(); 253 | // } 254 | // return decryptpath; 255 | // } 256 | // 257 | // @Override 258 | // protected void onPostExecute(String s) { 259 | // Toast.makeText(MainActivity.this, "" + selectedVideoPath + " Decrypted Successfully!!", Toast.LENGTH_SHORT).show(); 260 | // vdVideoView.stopPlayback(); 261 | // vdVideoView.setVideoPath(s); 262 | // vdVideoView.start(); 263 | // super.onPostExecute(s); 264 | // } 265 | // }.execute(); 266 | 267 | 268 | } 269 | }); 270 | 271 | } 272 | 273 | ArrayList> arrayList; 274 | 275 | public ArrayList> searchFile(File dir) { 276 | String pdfPattern = ".swf"; 277 | final File[] listFile = dir.listFiles(); 278 | 279 | if (listFile != null) { 280 | for (int i = 0; i < listFile.length; i++) { 281 | if (listFile[i].isDirectory()) { 282 | searchFile(listFile[i]); 283 | } 284 | else { 285 | if (listFile[i].getName().endsWith(pdfPattern)){ 286 | final int indx=i; 287 | arrayList = new ArrayList(); 288 | arrayList.add(new HashMap() 289 | { 290 | { 291 | put("data",listFile[indx].getAbsolutePath()); 292 | put("displayName",listFile[indx].getName()); 293 | } 294 | }); 295 | } 296 | } 297 | } 298 | } 299 | return arrayList; 300 | } 301 | private void refreshSDCard() 302 | { 303 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 304 | Intent mediaScanIntent = new Intent( 305 | Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 306 | Uri contentUri = Uri.fromFile(inFile); 307 | mediaScanIntent.setData(contentUri); 308 | sendBroadcast(mediaScanIntent); 309 | } else { 310 | sendBroadcast(new Intent( 311 | Intent.ACTION_MEDIA_MOUNTED, 312 | Uri.parse("file://" 313 | + Environment.getExternalStorageDirectory()))); 314 | } 315 | } 316 | 317 | 318 | 319 | 320 | @Override 321 | public void onReturningResult(Intent data) { 322 | Toast.makeText(MainActivity.this,"Result Received at activity",Toast.LENGTH_SHORT).show(); 323 | selectedVideoPath=data.getExtras().get("data").toString(); 324 | tvText.setText(selectedVideoPath); 325 | 326 | vdVideoView.setVideoPath(selectedVideoPath); 327 | vdVideoView.start(); 328 | 329 | 330 | } 331 | } 332 | 333 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/akash/videoencrptionexa/VideoDialogFragment.java: -------------------------------------------------------------------------------- 1 | package com.example.akash.videoencrptionexa; 2 | 3 | import android.app.Activity; 4 | import android.app.DialogFragment; 5 | import android.content.Intent; 6 | import android.database.Cursor; 7 | import android.os.Bundle; 8 | import android.support.annotation.Nullable; 9 | import android.support.v7.widget.LinearLayoutManager; 10 | import android.support.v7.widget.RecyclerView; 11 | import android.view.LayoutInflater; 12 | import android.view.View; 13 | import android.view.ViewGroup; 14 | import android.widget.Toast; 15 | 16 | import java.util.ArrayList; 17 | import java.util.HashMap; 18 | 19 | import butterknife.ButterKnife; 20 | import butterknife.InjectView; 21 | 22 | /** 23 | * Created by akash on 31/5/16. 24 | */ 25 | public class VideoDialogFragment extends DialogFragment { 26 | @InjectView(R.id.rvVideoDialogFragment) 27 | RecyclerView rvVideoDialogFragment; 28 | 29 | static VideoDialogFragment newInstance(ArrayList> arrayList) { 30 | VideoDialogFragment f = new VideoDialogFragment(); 31 | 32 | // Supply num input as an argument. 33 | Bundle args = new Bundle(); 34 | args.putSerializable("videoList", arrayList); 35 | f.setArguments(args); 36 | 37 | return f; 38 | } 39 | public interface DialogFragmentToActivity 40 | { 41 | public void onReturningResult(Intent data); 42 | } 43 | 44 | 45 | 46 | @Nullable 47 | @Override 48 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 49 | View v = inflater.inflate(R.layout.video_dialog_fragment,container,false); 50 | ButterKnife.inject(this,v); 51 | getDialog().setTitle("Select Video to Encrypt"); 52 | rvVideoDialogFragment.setLayoutManager(new LinearLayoutManager(getActivity())); 53 | VideoRecyclerAdapter videoRecyclerAdapter = new VideoRecyclerAdapter((ArrayList>)getArguments().getSerializable("videoList"),VideoDialogFragment.this); 54 | rvVideoDialogFragment.setAdapter(videoRecyclerAdapter); 55 | return v; 56 | } 57 | 58 | @Override 59 | public void onAttach(Activity activity) { 60 | super.onAttach(activity); 61 | 62 | } 63 | 64 | @Override 65 | public void onDetach() { 66 | super.onDetach(); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/akash/videoencrptionexa/VideoRecyclerAdapter.java: -------------------------------------------------------------------------------- 1 | package com.example.akash.videoencrptionexa; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.support.v7.widget.RecyclerView; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | import android.widget.ImageView; 10 | import android.widget.TextView; 11 | import android.widget.Toast; 12 | 13 | import java.util.ArrayList; 14 | import java.util.HashMap; 15 | 16 | /** 17 | * Created by akash on 31/5/16. 18 | */ 19 | public class VideoRecyclerAdapter extends RecyclerView.Adapter { 20 | ArrayList> listofVideos; 21 | Context context; 22 | VideoDialogFragment dialogReference; 23 | public VideoRecyclerAdapter(ArrayList> arrayList, VideoDialogFragment context) { 24 | listofVideos=new ArrayList<>(); 25 | listofVideos=arrayList; 26 | this.context=context.getActivity(); 27 | dialogReference =context; 28 | } 29 | 30 | @Override 31 | public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 32 | View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.video_recycler_items,null); 33 | final CustomViewHolder viewHolder =new CustomViewHolder(view); 34 | 35 | return viewHolder; 36 | } 37 | 38 | @Override 39 | public void onBindViewHolder(CustomViewHolder holder, int position) { 40 | holder.textView.setText(listofVideos.get(position).get("displayName")); 41 | 42 | } 43 | 44 | @Override 45 | public int getItemCount() { 46 | return (null!=listofVideos? listofVideos.size():0); 47 | } 48 | 49 | public class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener 50 | { 51 | protected ImageView imageView; 52 | protected TextView textView; 53 | public CustomViewHolder(View itemView) { 54 | super(itemView); 55 | itemView.setOnClickListener(this); 56 | imageView=(ImageView)itemView.findViewById(R.id.ivVideoImage); 57 | textView=(TextView)itemView.findViewById(R.id.tvVideoName); 58 | 59 | } 60 | 61 | 62 | @Override 63 | public void onClick(View view) { 64 | Intent intent = new Intent(); 65 | intent.putExtra("data",listofVideos.get(getAdapterPosition()).get("data")); 66 | intent.putExtra("displayName",listofVideos.get(getAdapterPosition()).get("displayName")); 67 | //dialogReference.getTargetFragment().onActivityResult(dialogReference.getTargetRequestCode(),100,intent); 68 | VideoDialogFragment.DialogFragmentToActivity dialogFragmentToActivity =(VideoDialogFragment.DialogFragmentToActivity)context; 69 | dialogFragmentToActivity.onReturningResult(intent); 70 | dialogReference.dismiss(); 71 | 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 |