├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── ic_launcher-web.png │ ├── java │ └── com │ │ └── snatik │ │ └── storage │ │ └── app │ │ ├── FilesAdapter.java │ │ ├── GenericFileProvider.java │ │ ├── Helper.java │ │ ├── MainActivity.java │ │ ├── MyApplication.java │ │ ├── ViewTextActivity.java │ │ └── dialogs │ │ ├── AddItemsDialog.java │ │ ├── ConfirmDeleteDialog.java │ │ ├── NewFolderDialog.java │ │ ├── NewTextFileDialog.java │ │ ├── RenameDialog.java │ │ └── UpdateItemDialog.java │ └── res │ ├── drawable │ ├── ic_add_white_24dp.xml │ ├── ic_content_copy_grey_24dp.xml │ ├── ic_create_new_folder_grey_24dp.xml │ ├── ic_delete_forever_grey_24dp.xml │ ├── ic_file_primary_24dp.xml │ ├── ic_folder_primary_24dp.xml │ ├── ic_mode_edit_grey_24dp.xml │ ├── ic_move_grey_24dp.xml │ └── ic_note_add_grey_24dp.xml │ ├── layout │ ├── activity_main.xml │ ├── activity_view_text_file.xml │ ├── add_items_dialog.xml │ ├── content_main.xml │ ├── file_line_view.xml │ ├── new_file_dialog.xml │ ├── new_folder_dialog.xml │ ├── rename_dialog.xml │ └── update_item_dialog.xml │ ├── menu │ ├── main_menu.xml │ ├── path_menu.xml │ └── text_menu.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-land │ └── dimens.xml │ ├── values │ ├── colors.xml │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml │ └── xml │ └── provider_paths.xml ├── assets └── sample_app.png ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── storage ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src ├── androidTest └── java │ └── com │ └── snatik │ └── storage │ └── StorageTestCase.java └── main ├── AndroidManifest.xml └── java └── com └── snatik └── storage ├── EncryptConfiguration.java ├── Storable.java ├── Storage.java ├── helpers ├── ImmutablePair.java ├── OrderType.java └── SizeUnit.java └── security ├── CipherAlgorithmType.java ├── CipherModeType.java ├── CipherPaddingType.java ├── CipherTransformationType.java └── SecurityUtil.java /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | local.properties 4 | .idea 5 | .DS_Store 6 | /build 7 | /captures 8 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | All notable changes to this project will be documented in this file. 3 | 4 | ## [2.1.0] - 2017-07-01 5 | ### Added 6 | - Sample app 7 | 8 | ### Changed 9 | - Refactored and changed package name to com.snatik.storage. 10 | - Configuration class changed to by security configuration. 11 | - All methods will work with absolute file and directory paths only. 12 | 13 | ### Removed 14 | - Internal and External classes implementations. 15 | - Test and library is used in sample app. 16 | - No more RuntimeException, but added logs and return boolean true/false upon execution. 17 | 18 | ### Fixed 19 | - Random salt is generated on each app session. 20 | 21 | 22 | ## [1.2.1] - 2017-05-22 23 | ### Added 24 | - Support for public directories. -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | android-storage 2 | ====================== 3 | 4 | Library to create, read, delete, append, encrypt files and more, on internal or external disk spaces with a really simple API. 5 | 6 | ## Latest Release 7 | 8 | [ ![Download](https://api.bintray.com/packages/sromku/maven/storage/images/download.svg) ](https://bintray.com/sromku/maven/storage/_latestVersion) 9 | 10 | ``` groovy 11 | dependencies { 12 | compile 'com.snatik:storage:2.1.0' 13 | } 14 | ``` 15 | 16 | Don't forget to update `AndroidManifest.xml` and add next line: 17 | 18 | ``` xml 19 | 20 | ``` 21 | 22 | ## Usage 23 | 24 | ``` java 25 | 26 | // init 27 | Storage storage = new Storage(getApplicationContext()); 28 | 29 | // get external storage 30 | String path = storage.getExternalStorageDirectory(); 31 | 32 | // new dir 33 | String newDir = path + File.separator + "My Sample Directory"; 34 | storage.createDirectory(newDir); 35 | ``` 36 | 37 | Check all options, scroll down ;) 38 | 39 | 40 | ## Sample app 41 | 42 | The app has some simple UI screens and uses the storage library. This is just an example of what can be done with this lib. 43 | 44 | 45 | 46 | ## Options 47 | * [Easy define Internal or External storage](#initialize) 48 | * [Create directory](#create-directory) 49 | * [Create file](#create-file) 50 | * [Read file content](#read-file) 51 | * [Append content to file](#append-content-to-file) 52 | * [Copy](#copy) 53 | * [Move](#move) 54 | * [Delete directory](#delete-directory) 55 | * [Delete file](#delete-file) 56 | * [Get files](#get-files) 57 | * [More options](#more) 58 | * [Encrypt the file content](#security-configuration) 59 | 60 | ### Initialize 61 | 62 | ``` 63 | Storage storage = new Storage(getApplicationContext()); 64 | ``` 65 | 66 | Work on **External Storage**. 67 | 68 | - Check if external writable 69 | 70 | ``` java 71 | boolean isWritable = storage.isExternalWritable(); 72 | ``` 73 | 74 | - Root external storage path 75 | 76 | ``` java 77 | String path = storage.getExternalStorageDirectory(); 78 | ``` 79 | 80 | - If you want to use a particular public directory 81 | 82 | ``` java 83 | Storage storage = SimpleStorage.getExternalStorage(Environment.DIRECTORY_PICTURES); 84 | ``` 85 | 86 | Work on **Internal Storage**. 87 | 88 | - Directory for storing app internal files ([documentation](https://developer.android.com/training/basics/data-storage/files.html#WriteInternalStorage)): 89 | 90 | ``` java 91 | String path = SimpleStorage.getInternalFilesDirectory(); 92 | ``` 93 | 94 | - Cache dir 95 | 96 | ``` java 97 | String path = SimpleStorage.getInternalCacheDirectory(); 98 | ``` 99 | 100 | - Root internal storage dir 101 | 102 | ``` java 103 | String path = SimpleStorage.getInternalRootDirectory(); 104 | ``` 105 | 106 | ### Create directory 107 | 108 | - Create directory 109 | 110 | ``` java 111 | storage.createDirectory(path); 112 | ``` 113 | 114 | - Create directory and **override** the existing one. 115 | 116 | ``` java 117 | storage.createDirectory(path, true); 118 | ``` 119 | 120 | ### Create file 121 | 122 | Create a new file with the content in it. 123 | 124 | ``` java 125 | storage.createFile(path, "some content of the file"); 126 | ``` 127 | 128 | The `content` of the file can be one of the next types: 129 | - `String` 130 | - `byte[]` 131 | - `Bitmap` 132 | - `Storable` 133 | 134 | ### Read file 135 | 136 | Read the content of any file to byte array. 137 | 138 | ``` java 139 | byte[] bytes = storage.readFile(path); 140 | ``` 141 | 142 | Read the content of the file to String. 143 | ``` java 144 | String content = storage.readTextFile(path); 145 | ``` 146 | 147 | ### Append content to file 148 | ``` java 149 | storage.appendFile(path, "more new data"); 150 | ``` 151 | 152 | You can append: 153 | - `String` 154 | - `byte[]` 155 | 156 | ### Copy 157 | ``` java 158 | storage.copy(fromPath, toPath); 159 | ``` 160 | 161 | ### Move 162 | ``` java 163 | storage.move(fromPath, toPath); 164 | ``` 165 | 166 | ### Delete directory 167 | ``` java 168 | storage.deleteDirectory(path); 169 | ``` 170 | 171 | ### Delete file 172 | ``` java 173 | storage.deleteFile(path); 174 | ``` 175 | 176 | ### Get files 177 | - Get files in ordered way by: `name`, `date`, `size` 178 | ``` java 179 | List files = storage.getFiles(path, OrderType.DATE); 180 | ``` 181 | 182 | - Get files and filter by regular expression: 183 | ``` java 184 | String regex = ...; 185 | List files = storage.getFiles(path, regex); 186 | ``` 187 | 188 | * Get all nested files (without the directories) 189 | ``` java 190 | List files = storage.getNestedFiles(path); 191 | ``` 192 | 193 | ### More... 194 | 195 | * Is directory exists 196 | ``` java 197 | boolean dirExists = storage.isDirectoryExists(path); 198 | ``` 199 | 200 | * Is file exists 201 | ``` java 202 | boolean fileExists = storage.isFileExist(path); 203 | ``` 204 | 205 | 206 | ## Security configuration 207 | You can write and read files while the content is **encrypted**. It means, that no one can read the data of your files from external or internal storage. 208 | 209 | You will continue using the same api as before. The only thing you need to do is to configure the Simple Storage library before the you want to create/read encrypted data. 210 | 211 | ``` java 212 | // set encryption 213 | String IVX = "abcdefghijklmnop"; // 16 lenght - not secret 214 | String SECRET_KEY = "secret1234567890"; // 16 lenght - secret 215 | byte[] SALT = "0000111100001111".getBytes(); // random 16 bytes array 216 | 217 | // build configuratio 218 | EncryptConfiguration configuration = new EncryptConfiguration.Builder() 219 | .setEncryptContent(IVX, SECRET_KEY, SALT) 220 | .build(); 221 | 222 | // configure the simple storage 223 | storage.setEncryptConfiguration(configuration); 224 | ``` 225 | 226 | Now, you can create a new file with content and the content will be automatically encrypted.
227 | You can read the file and the content will be decrypted. 228 | 229 | **Example** 230 | 231 | Create file with next content `"this is the secret data"`: 232 | ``` java 233 | storage.setEncryptConfiguration(configuration); 234 | storage.createFile(path, "this is the secret data"); 235 | ``` 236 | 237 | If we open the file to see it's content then it we will something like this: `„f°α�ΤG†_iΐp` . It looks good :) 238 | 239 | And now, read the file data with the same api: 240 | ``` java 241 | storage.setEncryptConfiguration(configuration); 242 | String content = storage.readTextFile(path); 243 | ``` 244 | You will see that the content will be: `"this is the secret data"`. 245 | 246 | ## Tests 247 | 248 | Just play and check the sample app ;) 249 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion '25.0.3' 6 | 7 | defaultConfig { 8 | applicationId "com.snatik.storage.sample" 9 | minSdkVersion 14 10 | targetSdkVersion 25 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(include: ['*.jar'], dir: 'libs') 24 | compile project(':storage') 25 | compile 'com.android.support.constraint:constraint-layout:1.0.2' 26 | compile 'com.android.support:design:25.3.1' 27 | } 28 | -------------------------------------------------------------------------------- /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 /Users/sromku/android/adt/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/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 14 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 26 | 27 | 32 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /app/src/main/ic_launcher-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sromku/android-storage/37134be795246772e7a8223c1a58708a395b6455/app/src/main/ic_launcher-web.png -------------------------------------------------------------------------------- /app/src/main/java/com/snatik/storage/app/FilesAdapter.java: -------------------------------------------------------------------------------- 1 | package com.snatik.storage.app; 2 | 3 | import android.content.Context; 4 | import android.support.v7.widget.RecyclerView; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | import android.widget.ImageView; 9 | import android.widget.TextView; 10 | 11 | import com.snatik.storage.Storage; 12 | 13 | import java.io.File; 14 | import java.util.List; 15 | 16 | /** 17 | * Created by sromku on June, 2017. 18 | */ 19 | public class FilesAdapter extends RecyclerView.Adapter { 20 | 21 | private List mFiles; 22 | private OnFileItemListener mListener; 23 | private Storage mStorage; 24 | 25 | public FilesAdapter(Context context) { 26 | mStorage = new Storage(context); 27 | } 28 | 29 | @Override 30 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 31 | View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.file_line_view, parent, false); 32 | return new FileViewHolder(view); 33 | } 34 | 35 | @Override 36 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 37 | final File file = mFiles.get(position); 38 | FileViewHolder fileViewHolder = (FileViewHolder) holder; 39 | fileViewHolder.itemView.setOnClickListener(new View.OnClickListener() { 40 | @Override 41 | public void onClick(View view) { 42 | mListener.onClick(file); 43 | } 44 | }); 45 | fileViewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() { 46 | @Override 47 | public boolean onLongClick(View view) { 48 | mListener.onLongClick(file); 49 | return true; 50 | } 51 | }); 52 | fileViewHolder.mName.setText(file.getName()); 53 | fileViewHolder.mIcon.setImageResource(file.isDirectory() ? R.drawable.ic_folder_primary_24dp : R.drawable 54 | .ic_file_primary_24dp); 55 | if (file.isDirectory()) { 56 | fileViewHolder.mSize.setVisibility(View.GONE); 57 | } else { 58 | fileViewHolder.mSize.setVisibility(View.VISIBLE); 59 | fileViewHolder.mSize.setText(mStorage.getReadableSize(file)); 60 | } 61 | 62 | } 63 | 64 | @Override 65 | public int getItemCount() { 66 | return mFiles != null ? mFiles.size() : 0; 67 | } 68 | 69 | public void setFiles(List files) { 70 | mFiles = files; 71 | } 72 | 73 | public void setListener(OnFileItemListener listener) { 74 | mListener = listener; 75 | } 76 | 77 | static class FileViewHolder extends RecyclerView.ViewHolder { 78 | 79 | TextView mName; 80 | TextView mSize; 81 | ImageView mIcon; 82 | 83 | FileViewHolder(View v) { 84 | super(v); 85 | mName = (TextView) v.findViewById(R.id.name); 86 | mSize = (TextView) v.findViewById(R.id.size); 87 | mIcon = (ImageView) v.findViewById(R.id.icon); 88 | } 89 | } 90 | 91 | public interface OnFileItemListener { 92 | void onClick(File file); 93 | 94 | void onLongClick(File file); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /app/src/main/java/com/snatik/storage/app/GenericFileProvider.java: -------------------------------------------------------------------------------- 1 | package com.snatik.storage.app; 2 | 3 | import android.support.v4.content.FileProvider; 4 | 5 | /** 6 | * Created by sromku on July, 2017. 7 | */ 8 | public class GenericFileProvider extends FileProvider { 9 | } 10 | -------------------------------------------------------------------------------- /app/src/main/java/com/snatik/storage/app/Helper.java: -------------------------------------------------------------------------------- 1 | package com.snatik.storage.app; 2 | 3 | import android.support.design.widget.Snackbar; 4 | import android.view.View; 5 | 6 | /** 7 | * Created by sromku on June, 2017. 8 | */ 9 | public class Helper { 10 | 11 | public static void showSnackbar(String message, View root) { 12 | Snackbar.make(root, message, Snackbar.LENGTH_SHORT).show(); 13 | } 14 | 15 | public static String fileExt(String url) { 16 | if (url.indexOf("?") > -1) { 17 | url = url.substring(0, url.indexOf("?")); 18 | } 19 | if (url.lastIndexOf(".") == -1) { 20 | return null; 21 | } else { 22 | String ext = url.substring(url.lastIndexOf(".") + 1); 23 | if (ext.indexOf("%") > -1) { 24 | ext = ext.substring(0, ext.indexOf("%")); 25 | } 26 | if (ext.indexOf("/") > -1) { 27 | ext = ext.substring(0, ext.indexOf("/")); 28 | } 29 | return ext.toLowerCase(); 30 | 31 | } 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /app/src/main/java/com/snatik/storage/app/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.snatik.storage.app; 2 | 3 | import android.Manifest; 4 | import android.content.ActivityNotFoundException; 5 | import android.content.Intent; 6 | import android.content.pm.PackageManager; 7 | import android.net.Uri; 8 | import android.os.Bundle; 9 | import android.support.annotation.NonNull; 10 | import android.support.v4.app.ActivityCompat; 11 | import android.support.v4.content.ContextCompat; 12 | import android.support.v4.content.FileProvider; 13 | import android.support.v7.app.AppCompatActivity; 14 | import android.support.v7.widget.LinearLayoutManager; 15 | import android.support.v7.widget.RecyclerView; 16 | import android.support.v7.widget.Toolbar; 17 | import android.view.MenuInflater; 18 | import android.view.MenuItem; 19 | import android.view.View; 20 | import android.webkit.MimeTypeMap; 21 | import android.widget.PopupMenu; 22 | import android.widget.TextView; 23 | 24 | import com.snatik.storage.EncryptConfiguration; 25 | import com.snatik.storage.Storage; 26 | import com.snatik.storage.app.dialogs.AddItemsDialog; 27 | import com.snatik.storage.app.dialogs.ConfirmDeleteDialog; 28 | import com.snatik.storage.app.dialogs.NewFolderDialog; 29 | import com.snatik.storage.app.dialogs.NewTextFileDialog; 30 | import com.snatik.storage.app.dialogs.RenameDialog; 31 | import com.snatik.storage.app.dialogs.UpdateItemDialog; 32 | import com.snatik.storage.helpers.OrderType; 33 | import com.snatik.storage.helpers.SizeUnit; 34 | 35 | import java.io.File; 36 | import java.util.Collections; 37 | import java.util.List; 38 | 39 | import static com.snatik.storage.app.Helper.fileExt; 40 | 41 | public class MainActivity extends AppCompatActivity implements 42 | FilesAdapter.OnFileItemListener, 43 | AddItemsDialog.DialogListener, 44 | UpdateItemDialog.DialogListener, 45 | NewFolderDialog.DialogListener, 46 | NewTextFileDialog.DialogListener, 47 | ConfirmDeleteDialog.ConfirmListener, 48 | RenameDialog.DialogListener { 49 | 50 | private static final int PERMISSION_REQUEST_CODE = 1000; 51 | private RecyclerView mRecyclerView; 52 | private FilesAdapter mFilesAdapter; 53 | private Storage mStorage; 54 | private TextView mPathView; 55 | private TextView mMovingText; 56 | private boolean mCopy; 57 | private View mMovingLayout; 58 | private int mTreeSteps = 0; 59 | private final static String IVX = "abcdefghijklmnop"; 60 | private final static String SECRET_KEY = "secret1234567890"; 61 | private final static byte[] SALT = "0000111100001111".getBytes(); 62 | private String mMovingPath; 63 | private boolean mInternal = false; 64 | 65 | @Override 66 | protected void onCreate(Bundle savedInstanceState) { 67 | super.onCreate(savedInstanceState); 68 | 69 | mStorage = new Storage(getApplicationContext()); 70 | 71 | setContentView(R.layout.activity_main); 72 | Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 73 | setSupportActionBar(toolbar); 74 | 75 | mRecyclerView = (RecyclerView) findViewById(R.id.recycler); 76 | mPathView = (TextView) findViewById(R.id.path); 77 | mMovingLayout = findViewById(R.id.moving_layout); 78 | mMovingText = (TextView) mMovingLayout.findViewById(R.id.moving_file_name); 79 | 80 | mMovingLayout.findViewById(R.id.accept_move).setOnClickListener(new View.OnClickListener() { 81 | @Override 82 | public void onClick(View view) { 83 | mMovingLayout.setVisibility(View.GONE); 84 | if (mMovingPath != null) { 85 | 86 | if (!mCopy) { 87 | String toPath = getCurrentPath() + File.separator + mStorage.getFile(mMovingPath).getName(); 88 | if (!mMovingPath.equals(toPath)) { 89 | mStorage.move(mMovingPath, toPath); 90 | Helper.showSnackbar("Moved", mRecyclerView); 91 | showFiles(getCurrentPath()); 92 | } else { 93 | Helper.showSnackbar("The file is already here", mRecyclerView); 94 | } 95 | } else { 96 | String toPath = getCurrentPath() + File.separator + "copy " + mStorage.getFile(mMovingPath) 97 | .getName(); 98 | mStorage.copy(mMovingPath, toPath); 99 | Helper.showSnackbar("Copied", mRecyclerView); 100 | showFiles(getCurrentPath()); 101 | } 102 | mMovingPath = null; 103 | } 104 | } 105 | }); 106 | 107 | mMovingLayout.findViewById(R.id.decline_move).setOnClickListener(new View.OnClickListener() { 108 | @Override 109 | public void onClick(View view) { 110 | mMovingLayout.setVisibility(View.GONE); 111 | mMovingPath = null; 112 | } 113 | }); 114 | 115 | RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this); 116 | mRecyclerView.setLayoutManager(layoutManager); 117 | mFilesAdapter = new FilesAdapter(getApplicationContext()); 118 | mFilesAdapter.setListener(this); 119 | mRecyclerView.setAdapter(mFilesAdapter); 120 | 121 | findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() { 122 | @Override 123 | public void onClick(View view) { 124 | AddItemsDialog.newInstance().show(getFragmentManager(), "add_items"); 125 | } 126 | }); 127 | 128 | mPathView.setOnClickListener(new View.OnClickListener() { 129 | @Override 130 | public void onClick(View view) { 131 | showPathMenu(); 132 | } 133 | }); 134 | 135 | // load files 136 | showFiles(mStorage.getExternalStorageDirectory()); 137 | 138 | checkPermission(); 139 | } 140 | 141 | private void showPathMenu() { 142 | PopupMenu popupmenu = new PopupMenu(this, mPathView); 143 | MenuInflater inflater = popupmenu.getMenuInflater(); 144 | inflater.inflate(R.menu.path_menu, popupmenu.getMenu()); 145 | 146 | popupmenu.getMenu().findItem(R.id.go_internal).setVisible(!mInternal); 147 | popupmenu.getMenu().findItem(R.id.go_external).setVisible(mInternal); 148 | 149 | popupmenu.show(); 150 | 151 | popupmenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { 152 | 153 | @Override 154 | public boolean onMenuItemClick(MenuItem item) { 155 | switch (item.getItemId()) { 156 | case R.id.go_up: 157 | String previousPath = getPreviousPath(); 158 | mTreeSteps = 0; 159 | showFiles(previousPath); 160 | break; 161 | case R.id.go_internal: 162 | showFiles(mStorage.getInternalFilesDirectory()); 163 | mInternal = true; 164 | break; 165 | case R.id.go_external: 166 | showFiles(mStorage.getExternalStorageDirectory()); 167 | mInternal = false; 168 | break; 169 | } 170 | return true; 171 | } 172 | }); 173 | } 174 | 175 | private void showFiles(String path) { 176 | mPathView.setText(path); 177 | List files = mStorage.getFiles(path); 178 | if (files != null) { 179 | Collections.sort(files, OrderType.NAME.getComparator()); 180 | } 181 | mFilesAdapter.setFiles(files); 182 | mFilesAdapter.notifyDataSetChanged(); 183 | } 184 | 185 | 186 | @Override 187 | public void onClick(File file) { 188 | if (file.isDirectory()) { 189 | mTreeSteps++; 190 | String path = file.getAbsolutePath(); 191 | showFiles(path); 192 | } else { 193 | 194 | try { 195 | Intent intent = new Intent(Intent.ACTION_VIEW); 196 | String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExt(file.getAbsolutePath())); 197 | Uri apkURI = FileProvider.getUriForFile( 198 | this, 199 | getApplicationContext() 200 | .getPackageName() + ".provider", file); 201 | intent.setDataAndType(apkURI, mimeType); 202 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 203 | startActivity(intent); 204 | } catch (ActivityNotFoundException e) { 205 | if (mStorage.getSize(file, SizeUnit.KB) > 500) { 206 | Helper.showSnackbar("The file is too big for preview", mRecyclerView); 207 | return; 208 | } 209 | Intent intent = new Intent(this, ViewTextActivity.class); 210 | intent.putExtra(ViewTextActivity.EXTRA_FILE_NAME, file.getName()); 211 | intent.putExtra(ViewTextActivity.EXTRA_FILE_PATH, file.getAbsolutePath()); 212 | startActivity(intent); 213 | } 214 | 215 | } 216 | } 217 | 218 | @Override 219 | public void onLongClick(File file) { 220 | UpdateItemDialog.newInstance(file.getAbsolutePath()).show(getFragmentManager(), "update_item"); 221 | } 222 | 223 | @Override 224 | public void onBackPressed() { 225 | if (mTreeSteps > 0) { 226 | String path = getPreviousPath(); 227 | mTreeSteps--; 228 | showFiles(path); 229 | return; 230 | } 231 | super.onBackPressed(); 232 | } 233 | 234 | private String getCurrentPath() { 235 | return mPathView.getText().toString(); 236 | } 237 | 238 | private String getPreviousPath() { 239 | String path = getCurrentPath(); 240 | int lastIndexOf = path.lastIndexOf(File.separator); 241 | if (lastIndexOf < 0) { 242 | Helper.showSnackbar("Can't go anymore", mRecyclerView); 243 | return getCurrentPath(); 244 | } 245 | return path.substring(0, lastIndexOf); 246 | } 247 | 248 | @Override 249 | public void onOptionClick(int which, String path) { 250 | switch (which) { 251 | case R.id.new_file: 252 | NewTextFileDialog.newInstance().show(getFragmentManager(), "new_file_dialog"); 253 | break; 254 | case R.id.new_folder: 255 | NewFolderDialog.newInstance().show(getFragmentManager(), "new_folder_dialog"); 256 | break; 257 | case R.id.delete: 258 | ConfirmDeleteDialog.newInstance(path).show(getFragmentManager(), "confirm_delete"); 259 | break; 260 | case R.id.rename: 261 | RenameDialog.newInstance(path).show(getFragmentManager(), "rename"); 262 | break; 263 | case R.id.move: 264 | mMovingText.setText(getString(R.string.moving_file, mStorage.getFile(path).getName())); 265 | mMovingPath = path; 266 | mCopy = false; 267 | mMovingLayout.setVisibility(View.VISIBLE); 268 | break; 269 | case R.id.copy: 270 | mMovingText.setText(getString(R.string.copy_file, mStorage.getFile(path).getName())); 271 | mMovingPath = path; 272 | mCopy = true; 273 | mMovingLayout.setVisibility(View.VISIBLE); 274 | break; 275 | } 276 | } 277 | 278 | @Override 279 | public void onNewFolder(String name) { 280 | String currentPath = getCurrentPath(); 281 | String folderPath = currentPath + File.separator + name; 282 | boolean created = mStorage.createDirectory(folderPath); 283 | if (created) { 284 | showFiles(currentPath); 285 | Helper.showSnackbar("New folder created: " + name, mRecyclerView); 286 | } else { 287 | Helper.showSnackbar("Failed create folder: " + name, mRecyclerView); 288 | } 289 | } 290 | 291 | @Override 292 | public void onNewFile(String name, String content, boolean encrypted) { 293 | String currentPath = getCurrentPath(); 294 | String folderPath = currentPath + File.separator + name; 295 | if (encrypted) { 296 | mStorage.setEncryptConfiguration(new EncryptConfiguration.Builder() 297 | .setEncryptContent(IVX, SECRET_KEY, SALT) 298 | .build()); 299 | } 300 | mStorage.createFile(folderPath, content); 301 | showFiles(currentPath); 302 | Helper.showSnackbar("New file created: " + name, mRecyclerView); 303 | } 304 | 305 | @Override 306 | public void onConfirmDelete(String path) { 307 | if (mStorage.getFile(path).isDirectory()) { 308 | mStorage.deleteDirectory(path); 309 | Helper.showSnackbar("Folder was deleted", mRecyclerView); 310 | } else { 311 | mStorage.deleteFile(path); 312 | Helper.showSnackbar("File was deleted", mRecyclerView); 313 | } 314 | showFiles(getCurrentPath()); 315 | } 316 | 317 | @Override 318 | public void onRename(String fromPath, String toPath) { 319 | mStorage.rename(fromPath, toPath); 320 | showFiles(getCurrentPath()); 321 | Helper.showSnackbar("Renamed", mRecyclerView); 322 | } 323 | 324 | // @Override 325 | // public boolean onCreateOptionsMenu(Menu menu) { 326 | // MenuInflater inflater = getMenuInflater(); 327 | // inflater.inflate(R.menu.main_menu, menu); 328 | // return true; 329 | // } 330 | // 331 | // @Override 332 | // public boolean onOptionsItemSelected(MenuItem item) { 333 | // switch (item.getItemId()) { 334 | // case R.id.order: 335 | // break; 336 | // case R.id.filter: 337 | // break; 338 | // } 339 | // 340 | // return super.onOptionsItemSelected(item); 341 | // } 342 | 343 | private void checkPermission() { 344 | if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager 345 | .PERMISSION_GRANTED) { 346 | ActivityCompat.requestPermissions(this, 347 | new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 348 | PERMISSION_REQUEST_CODE); 349 | } 350 | } 351 | 352 | @Override 353 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] 354 | grantResults) { 355 | if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 356 | showFiles(mStorage.getExternalStorageDirectory()); 357 | } else { 358 | finish(); 359 | } 360 | } 361 | } 362 | -------------------------------------------------------------------------------- /app/src/main/java/com/snatik/storage/app/MyApplication.java: -------------------------------------------------------------------------------- 1 | package com.snatik.storage.app; 2 | 3 | import android.app.Application; 4 | 5 | public class MyApplication extends Application { 6 | 7 | public static String TAG = "MyApplication"; 8 | 9 | @Override 10 | public void onCreate() { 11 | super.onCreate(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /app/src/main/java/com/snatik/storage/app/ViewTextActivity.java: -------------------------------------------------------------------------------- 1 | package com.snatik.storage.app; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.support.v7.graphics.drawable.DrawerArrowDrawable; 7 | import android.support.v7.widget.Toolbar; 8 | import android.view.Menu; 9 | import android.view.MenuInflater; 10 | import android.view.MenuItem; 11 | import android.widget.TextView; 12 | 13 | import com.snatik.storage.EncryptConfiguration; 14 | import com.snatik.storage.Storage; 15 | 16 | /** 17 | * Created by sromku on June, 2017. 18 | */ 19 | public class ViewTextActivity extends AppCompatActivity { 20 | 21 | public final static String EXTRA_FILE_NAME = "name"; 22 | public final static String EXTRA_FILE_PATH = "path"; 23 | 24 | private final static String IVX = "abcdefghijklmnop"; 25 | private final static String SECRET_KEY = "secret1234567890"; 26 | private final static byte[] SALT = "0000111100001111".getBytes(); 27 | 28 | private TextView mContentView; 29 | private String mPath; 30 | private Storage mStorage; 31 | 32 | @Override 33 | protected void onCreate(@Nullable Bundle savedInstanceState) { 34 | super.onCreate(savedInstanceState); 35 | String name = getIntent().getStringExtra(EXTRA_FILE_NAME); 36 | mPath = getIntent().getStringExtra(EXTRA_FILE_PATH); 37 | 38 | setContentView(R.layout.activity_view_text_file); 39 | Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 40 | DrawerArrowDrawable drawerDrawable = new DrawerArrowDrawable(this); 41 | drawerDrawable.setColor(getResources().getColor(android.R.color.white)); 42 | drawerDrawable.setProgress(1f); 43 | toolbar.setNavigationIcon(drawerDrawable); 44 | 45 | setSupportActionBar(toolbar); 46 | getSupportActionBar().setDisplayHomeAsUpEnabled(true); 47 | getSupportActionBar().setTitle(name); 48 | getSupportActionBar().setHomeButtonEnabled(true); 49 | 50 | mContentView = (TextView) findViewById(R.id.content); 51 | mStorage = new Storage(this); 52 | byte[] bytes = mStorage.readFile(mPath); 53 | mContentView.setText(new String(bytes)); 54 | } 55 | 56 | @Override 57 | public boolean onCreateOptionsMenu(Menu menu) { 58 | MenuInflater inflater = getMenuInflater(); 59 | inflater.inflate(R.menu.text_menu, menu); 60 | return true; 61 | } 62 | 63 | @Override 64 | public boolean onOptionsItemSelected(MenuItem item) { 65 | switch (item.getItemId()) { 66 | case android.R.id.home: 67 | onBackPressed(); 68 | return true; 69 | case R.id.decrypt: 70 | mStorage.setEncryptConfiguration(new EncryptConfiguration.Builder() 71 | .setEncryptContent(IVX, SECRET_KEY, SALT) 72 | .build()); 73 | byte[] bytes = mStorage.readFile(mPath); 74 | if (bytes != null) { 75 | mContentView.setText(new String(bytes)); 76 | } else { 77 | Helper.showSnackbar("Failed to decrypt", mContentView); 78 | } 79 | break; 80 | } 81 | 82 | return super.onOptionsItemSelected(item); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /app/src/main/java/com/snatik/storage/app/dialogs/AddItemsDialog.java: -------------------------------------------------------------------------------- 1 | package com.snatik.storage.app.dialogs; 2 | 3 | import android.app.Activity; 4 | import android.app.Dialog; 5 | import android.app.DialogFragment; 6 | import android.content.DialogInterface; 7 | import android.os.Bundle; 8 | import android.support.design.widget.BottomSheetDialog; 9 | import android.view.LayoutInflater; 10 | import android.view.View; 11 | import android.view.ViewGroup; 12 | 13 | import com.snatik.storage.app.R; 14 | 15 | public class AddItemsDialog extends DialogFragment { 16 | 17 | private DialogListener mListener; 18 | 19 | public static AddItemsDialog newInstance() { 20 | AddItemsDialog fragment = new AddItemsDialog(); 21 | return fragment; 22 | } 23 | 24 | public AddItemsDialog() { 25 | } 26 | 27 | @Override 28 | public Dialog onCreateDialog(Bundle savedInstanceState) { 29 | 30 | final Dialog dialog = new BottomSheetDialog(getActivity(), getTheme()); 31 | 32 | View view = LayoutInflater.from(getActivity()).inflate(R.layout.add_items_dialog, null); 33 | dialog.setContentView(view); 34 | dialog.setCancelable(true); 35 | 36 | view.findViewById(R.id.new_folder).setOnClickListener(new View.OnClickListener() { 37 | @Override 38 | public void onClick(View view) { 39 | dialog.dismiss(); 40 | mListener.onOptionClick(R.id.new_folder, null); 41 | } 42 | }); 43 | 44 | view.findViewById(R.id.new_file).setOnClickListener(new View.OnClickListener() { 45 | @Override 46 | public void onClick(View view) { 47 | dialog.dismiss(); 48 | mListener.onOptionClick(R.id.new_file, null); 49 | } 50 | }); 51 | 52 | // control dialog width on different devices 53 | dialog.setOnShowListener(new DialogInterface.OnShowListener() { 54 | @Override 55 | public void onShow(DialogInterface dialogINterface) { 56 | int width = (int) getResources().getDimension(R.dimen.bottom_sheet_dialog_width); 57 | dialog.getWindow().setLayout( 58 | width == 0 ? ViewGroup.LayoutParams.MATCH_PARENT : width, 59 | ViewGroup.LayoutParams.MATCH_PARENT); 60 | } 61 | }); 62 | 63 | return dialog; 64 | } 65 | 66 | public interface DialogListener { 67 | void onOptionClick(int which, String path); 68 | } 69 | 70 | @Override 71 | public void onAttach(Activity activity) { 72 | super.onAttach(activity); 73 | try { 74 | mListener = (DialogListener) activity; 75 | } catch (ClassCastException e) { 76 | throw new ClassCastException(activity.toString() 77 | + " must implement DialogListener"); 78 | } 79 | } 80 | 81 | @Override 82 | public void onDetach() { 83 | super.onDetach(); 84 | mListener = null; 85 | } 86 | 87 | 88 | } 89 | -------------------------------------------------------------------------------- /app/src/main/java/com/snatik/storage/app/dialogs/ConfirmDeleteDialog.java: -------------------------------------------------------------------------------- 1 | package com.snatik.storage.app.dialogs; 2 | 3 | import android.app.AlertDialog; 4 | import android.app.Dialog; 5 | import android.app.DialogFragment; 6 | import android.content.DialogInterface; 7 | import android.os.Bundle; 8 | 9 | import com.snatik.storage.Storage; 10 | import com.snatik.storage.app.R; 11 | 12 | import java.io.File; 13 | 14 | public class ConfirmDeleteDialog extends DialogFragment { 15 | 16 | private final static String PATH = "path"; 17 | private String mPath; 18 | 19 | public static ConfirmDeleteDialog newInstance(String path) { 20 | ConfirmDeleteDialog fragment = new ConfirmDeleteDialog(); 21 | Bundle args = new Bundle(); 22 | args.putString(PATH, path); 23 | fragment.setArguments(args); 24 | return fragment; 25 | } 26 | 27 | @Override 28 | public Dialog onCreateDialog(Bundle savedInstanceState) { 29 | AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 30 | 31 | String msg; 32 | final String path = getArguments().getString(PATH); 33 | Storage storage = new Storage(getActivity()); 34 | File file = storage.getFile(path); 35 | if (file.isDirectory()) { 36 | msg = "You are about to delete the folder with all it's content for real."; 37 | } else { 38 | msg = "You are about to delete the file"; 39 | } 40 | builder.setMessage(msg); 41 | builder.setPositiveButton(R.string.label_delete, new DialogInterface.OnClickListener() { 42 | @Override 43 | public void onClick(DialogInterface dialog, int which) { 44 | ((ConfirmListener) getActivity()).onConfirmDelete(path); 45 | } 46 | }); 47 | builder.setNegativeButton(R.string.label_cancel, null); 48 | return builder.create(); 49 | } 50 | 51 | public interface ConfirmListener { 52 | void onConfirmDelete(String path); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /app/src/main/java/com/snatik/storage/app/dialogs/NewFolderDialog.java: -------------------------------------------------------------------------------- 1 | package com.snatik.storage.app.dialogs; 2 | 3 | import android.app.Activity; 4 | import android.app.AlertDialog; 5 | import android.app.Dialog; 6 | import android.app.DialogFragment; 7 | import android.content.DialogInterface; 8 | import android.os.Bundle; 9 | import android.text.Editable; 10 | import android.text.TextWatcher; 11 | import android.view.LayoutInflater; 12 | import android.view.View; 13 | import android.view.ViewGroup; 14 | import android.widget.EditText; 15 | 16 | import com.snatik.storage.app.R; 17 | 18 | /** 19 | * Created by sromku on June, 2017. 20 | */ 21 | public class NewFolderDialog extends DialogFragment { 22 | 23 | private NewFolderDialog.DialogListener mListener; 24 | 25 | public static NewFolderDialog newInstance() { 26 | NewFolderDialog fragment = new NewFolderDialog(); 27 | return fragment; 28 | } 29 | 30 | public NewFolderDialog() { 31 | } 32 | 33 | @Override 34 | public Dialog onCreateDialog(Bundle savedInstanceState) { 35 | 36 | AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 37 | 38 | final View view = LayoutInflater.from(getActivity()) 39 | .inflate(R.layout.new_folder_dialog, (ViewGroup) getView(), false); 40 | 41 | // if text is empty, disable the dialog positive button 42 | final EditText editText = (EditText) view.findViewById(R.id.name); 43 | editText.addTextChangedListener(new TextWatcher() { 44 | @Override 45 | public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 46 | } 47 | 48 | @Override 49 | public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 50 | } 51 | 52 | @Override 53 | public void afterTextChanged(Editable editable) { 54 | ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(editable != null && 55 | editable.length() > 0); 56 | } 57 | }); 58 | 59 | builder.setTitle(R.string.new_folder); 60 | builder.setView(view); 61 | builder.setPositiveButton(R.string.label_save, new DialogInterface.OnClickListener() { 62 | @Override 63 | public void onClick(DialogInterface dialogInterface, int i) { 64 | mListener.onNewFolder(editText.getText().toString()); 65 | } 66 | }); 67 | 68 | final AlertDialog dialog = builder.create(); 69 | view.post(new Runnable() { 70 | @Override 71 | public void run() { 72 | dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false); 73 | } 74 | }); 75 | dialog.setCancelable(false); 76 | return dialog; 77 | } 78 | 79 | public interface DialogListener { 80 | void onNewFolder(String name); 81 | } 82 | 83 | @Override 84 | public void onAttach(Activity activity) { 85 | super.onAttach(activity); 86 | try { 87 | mListener = (DialogListener) activity; 88 | } catch (ClassCastException e) { 89 | throw new ClassCastException(activity.toString() 90 | + " must implement DialogListener"); 91 | } 92 | } 93 | 94 | @Override 95 | public void onDetach() { 96 | super.onDetach(); 97 | mListener = null; 98 | } 99 | 100 | } 101 | -------------------------------------------------------------------------------- /app/src/main/java/com/snatik/storage/app/dialogs/NewTextFileDialog.java: -------------------------------------------------------------------------------- 1 | package com.snatik.storage.app.dialogs; 2 | 3 | import android.app.Activity; 4 | import android.app.AlertDialog; 5 | import android.app.Dialog; 6 | import android.app.DialogFragment; 7 | import android.content.DialogInterface; 8 | import android.os.Bundle; 9 | import android.text.Editable; 10 | import android.text.TextWatcher; 11 | import android.view.LayoutInflater; 12 | import android.view.View; 13 | import android.view.ViewGroup; 14 | import android.widget.CheckBox; 15 | import android.widget.EditText; 16 | 17 | import com.snatik.storage.app.R; 18 | 19 | /** 20 | * Created by sromku on June, 2017. 21 | */ 22 | public class NewTextFileDialog extends DialogFragment { 23 | 24 | private NewTextFileDialog.DialogListener mListener; 25 | 26 | public static NewTextFileDialog newInstance() { 27 | NewTextFileDialog fragment = new NewTextFileDialog(); 28 | return fragment; 29 | } 30 | 31 | public NewTextFileDialog() { 32 | } 33 | 34 | @Override 35 | public Dialog onCreateDialog(Bundle savedInstanceState) { 36 | 37 | AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 38 | 39 | final View view = LayoutInflater.from(getActivity()) 40 | .inflate(R.layout.new_file_dialog, (ViewGroup) getView(), false); 41 | 42 | // if text is empty, disable the dialog positive button 43 | final EditText nameEditText = (EditText) view.findViewById(R.id.name); 44 | final EditText contentEditText = (EditText) view.findViewById(R.id.content); 45 | final CheckBox encryptCheckbox = (CheckBox) view.findViewById(R.id.checkbox); 46 | 47 | nameEditText.addTextChangedListener(new TextWatcher() { 48 | @Override 49 | public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 50 | } 51 | 52 | @Override 53 | public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 54 | } 55 | 56 | @Override 57 | public void afterTextChanged(Editable editable) { 58 | ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(editable.length() > 0 && contentEditText.getText().length() > 0); 59 | } 60 | }); 61 | 62 | contentEditText.addTextChangedListener(new TextWatcher() { 63 | @Override 64 | public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 65 | } 66 | 67 | @Override 68 | public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 69 | } 70 | 71 | @Override 72 | public void afterTextChanged(Editable editable) { 73 | ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(editable.length() > 0 && nameEditText.getText().length() > 0); 74 | } 75 | }); 76 | 77 | builder.setTitle(R.string.new_file); 78 | builder.setView(view); 79 | builder.setPositiveButton(R.string.label_save, new DialogInterface.OnClickListener() { 80 | @Override 81 | public void onClick(DialogInterface dialogInterface, int i) { 82 | mListener.onNewFile(nameEditText.getText().toString(), contentEditText.getText().toString(), encryptCheckbox.isChecked()); 83 | } 84 | }); 85 | 86 | final AlertDialog dialog = builder.create(); 87 | view.post(new Runnable() { 88 | @Override 89 | public void run() { 90 | dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false); 91 | } 92 | }); 93 | dialog.setCancelable(false); 94 | return dialog; 95 | } 96 | 97 | public interface DialogListener { 98 | void onNewFile(String name, String content, boolean encrypt); 99 | } 100 | 101 | @Override 102 | public void onAttach(Activity activity) { 103 | super.onAttach(activity); 104 | try { 105 | mListener = (DialogListener) activity; 106 | } catch (ClassCastException e) { 107 | throw new ClassCastException(activity.toString() 108 | + " must implement DialogListener"); 109 | } 110 | } 111 | 112 | @Override 113 | public void onDetach() { 114 | super.onDetach(); 115 | mListener = null; 116 | } 117 | 118 | } 119 | -------------------------------------------------------------------------------- /app/src/main/java/com/snatik/storage/app/dialogs/RenameDialog.java: -------------------------------------------------------------------------------- 1 | package com.snatik.storage.app.dialogs; 2 | 3 | import android.app.Activity; 4 | import android.app.AlertDialog; 5 | import android.app.Dialog; 6 | import android.app.DialogFragment; 7 | import android.content.DialogInterface; 8 | import android.os.Bundle; 9 | import android.text.Editable; 10 | import android.text.TextWatcher; 11 | import android.view.LayoutInflater; 12 | import android.view.View; 13 | import android.view.ViewGroup; 14 | import android.widget.EditText; 15 | 16 | import com.snatik.storage.Storage; 17 | import com.snatik.storage.app.R; 18 | 19 | import java.io.File; 20 | 21 | /** 22 | * Created by sromku on June, 2017. 23 | */ 24 | public class RenameDialog extends DialogFragment { 25 | 26 | private final static String PATH = "path"; 27 | private DialogListener mListener; 28 | private Storage mStorage; 29 | 30 | public static RenameDialog newInstance(String path) { 31 | RenameDialog fragment = new RenameDialog(); 32 | Bundle args = new Bundle(); 33 | args.putString(PATH, path); 34 | fragment.setArguments(args); 35 | return fragment; 36 | } 37 | 38 | public RenameDialog() { 39 | } 40 | 41 | @Override 42 | public Dialog onCreateDialog(Bundle savedInstanceState) { 43 | 44 | mStorage = new Storage(getActivity()); 45 | 46 | AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 47 | 48 | final View view = LayoutInflater.from(getActivity()) 49 | .inflate(R.layout.rename_dialog, (ViewGroup) getView(), false); 50 | 51 | // if text is empty, disable the dialog positive button 52 | final EditText currentNameText = (EditText) view.findViewById(R.id.current_name); 53 | String path = getArguments().getString(PATH); 54 | 55 | final File file = mStorage.getFile(path); 56 | currentNameText.setText(file.getName()); 57 | final String parent = file.getParent(); 58 | 59 | final EditText newNameText = (EditText) view.findViewById(R.id.new_name); 60 | newNameText.addTextChangedListener(new TextWatcher() { 61 | @Override 62 | public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 63 | } 64 | 65 | @Override 66 | public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 67 | } 68 | 69 | @Override 70 | public void afterTextChanged(Editable editable) { 71 | ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(editable != null && 72 | editable.length() > 0); 73 | } 74 | }); 75 | 76 | builder.setTitle(R.string.rename); 77 | builder.setView(view); 78 | builder.setPositiveButton(R.string.label_save, new DialogInterface.OnClickListener() { 79 | @Override 80 | public void onClick(DialogInterface dialogInterface, int i) { 81 | String newName = newNameText.getText().toString(); 82 | String toPath = parent == null ? newName : parent + File.separator + newName; 83 | mListener.onRename(file.getPath(), toPath); 84 | } 85 | }); 86 | 87 | final AlertDialog dialog = builder.create(); 88 | view.post(new Runnable() { 89 | @Override 90 | public void run() { 91 | dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false); 92 | } 93 | }); 94 | dialog.setCancelable(false); 95 | return dialog; 96 | } 97 | 98 | public interface DialogListener { 99 | void onRename(String fromPath, String toPath); 100 | } 101 | 102 | @Override 103 | public void onAttach(Activity activity) { 104 | super.onAttach(activity); 105 | try { 106 | mListener = (DialogListener) activity; 107 | } catch (ClassCastException e) { 108 | throw new ClassCastException(activity.toString() 109 | + " must implement DialogListener"); 110 | } 111 | } 112 | 113 | @Override 114 | public void onDetach() { 115 | super.onDetach(); 116 | mListener = null; 117 | } 118 | 119 | } 120 | -------------------------------------------------------------------------------- /app/src/main/java/com/snatik/storage/app/dialogs/UpdateItemDialog.java: -------------------------------------------------------------------------------- 1 | package com.snatik.storage.app.dialogs; 2 | 3 | import android.app.Activity; 4 | import android.app.Dialog; 5 | import android.app.DialogFragment; 6 | import android.content.DialogInterface; 7 | import android.os.Bundle; 8 | import android.support.design.widget.BottomSheetDialog; 9 | import android.view.LayoutInflater; 10 | import android.view.View; 11 | import android.view.ViewGroup; 12 | import android.widget.TextView; 13 | 14 | import com.snatik.storage.Storage; 15 | import com.snatik.storage.app.R; 16 | 17 | public class UpdateItemDialog extends DialogFragment { 18 | 19 | private final static String PATH = "path"; 20 | private DialogListener mListener; 21 | 22 | public static UpdateItemDialog newInstance(String path) { 23 | UpdateItemDialog fragment = new UpdateItemDialog(); 24 | Bundle args = new Bundle(); 25 | args.putString(PATH, path); 26 | fragment.setArguments(args); 27 | return fragment; 28 | } 29 | 30 | public UpdateItemDialog() { 31 | } 32 | 33 | @Override 34 | public Dialog onCreateDialog(Bundle savedInstanceState) { 35 | 36 | final Dialog dialog = new BottomSheetDialog(getActivity(), getTheme()); 37 | final String path = getArguments().getString(PATH); 38 | boolean isDirectory = new Storage(getActivity()).getFile(path).isDirectory(); 39 | View view = LayoutInflater.from(getActivity()).inflate(R.layout.update_item_dialog, null); 40 | dialog.setContentView(view); 41 | dialog.setCancelable(true); 42 | 43 | // title 44 | TextView title = (TextView) view.findViewById(R.id.title); 45 | title.setText(isDirectory ? getString(R.string.folder_options) : getString(R.string.file_options)); 46 | 47 | View rename = view.findViewById(R.id.rename); 48 | View delete = view.findViewById(R.id.delete); 49 | View move = view.findViewById(R.id.move); 50 | View copy = view.findViewById(R.id.copy); 51 | 52 | rename.setOnClickListener(new View.OnClickListener() { 53 | @Override 54 | public void onClick(View view) { 55 | dialog.dismiss(); 56 | mListener.onOptionClick(R.id.rename, path); 57 | } 58 | }); 59 | 60 | delete.setOnClickListener(new View.OnClickListener() { 61 | @Override 62 | public void onClick(View view) { 63 | dialog.dismiss(); 64 | mListener.onOptionClick(R.id.delete, path); 65 | } 66 | }); 67 | 68 | if (!isDirectory) { 69 | move.setOnClickListener(new View.OnClickListener() { 70 | @Override 71 | public void onClick(View view) { 72 | dialog.dismiss(); 73 | mListener.onOptionClick(R.id.move, path); 74 | } 75 | }); 76 | 77 | copy.setOnClickListener(new View.OnClickListener() { 78 | @Override 79 | public void onClick(View view) { 80 | dialog.dismiss(); 81 | mListener.onOptionClick(R.id.copy, path); 82 | } 83 | }); 84 | } else { 85 | move.setVisibility(View.GONE); 86 | copy.setVisibility(View.GONE); 87 | } 88 | 89 | // control dialog width on different devices 90 | dialog.setOnShowListener(new DialogInterface.OnShowListener() { 91 | @Override 92 | public void onShow(DialogInterface dialogINterface) { 93 | int width = (int) getResources().getDimension(R.dimen.bottom_sheet_dialog_width); 94 | dialog.getWindow().setLayout( 95 | width == 0 ? ViewGroup.LayoutParams.MATCH_PARENT : width, 96 | ViewGroup.LayoutParams.MATCH_PARENT); 97 | } 98 | }); 99 | 100 | return dialog; 101 | } 102 | 103 | public interface DialogListener { 104 | void onOptionClick(int which, String path); 105 | } 106 | 107 | @Override 108 | public void onAttach(Activity activity) { 109 | super.onAttach(activity); 110 | try { 111 | mListener = (DialogListener) activity; 112 | } catch (ClassCastException e) { 113 | throw new ClassCastException(activity.toString() 114 | + " must implement DialogListener"); 115 | } 116 | } 117 | 118 | @Override 119 | public void onDetach() { 120 | super.onDetach(); 121 | mListener = null; 122 | } 123 | 124 | 125 | } 126 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_add_white_24dp.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_content_copy_grey_24dp.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_create_new_folder_grey_24dp.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_delete_forever_grey_24dp.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_file_primary_24dp.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_folder_primary_24dp.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_mode_edit_grey_24dp.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_move_grey_24dp.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_note_add_grey_24dp.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 13 | 14 | 20 | 21 | 22 | 23 | 24 | 25 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_view_text_file.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 12 | 13 | 19 | 20 | 21 | 22 | 25 | 26 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /app/src/main/res/layout/add_items_dialog.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 17 | 18 | 28 | 29 | 33 | 34 | 45 | 46 | 47 | 57 | 58 | 62 | 63 | 74 | 75 | 76 | -------------------------------------------------------------------------------- /app/src/main/res/layout/content_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 15 | 16 | 23 | 24 | 32 | 33 | 38 | 39 |