|
2 | 2 |
|
3 | 3 | import android.annotation.SuppressLint;
|
4 | 4 | import android.app.FragmentTransaction;
|
| 5 | +import android.content.ContentResolver; |
5 | 6 | import android.content.Context;
|
| 7 | +import android.content.Intent; |
6 | 8 | import android.content.SharedPreferences;
|
| 9 | +import android.net.Uri; |
7 | 10 | import android.os.Bundle;
|
8 | 11 | import android.preference.Preference;
|
9 | 12 | import android.preference.PreferenceFragment;
|
10 | 13 | import android.preference.PreferenceScreen;
|
| 14 | +import android.provider.MediaStore; |
11 | 15 | import android.support.design.widget.AppBarLayout;
|
12 | 16 | import android.support.v7.app.AppCompatActivity;
|
13 | 17 | import android.support.v7.widget.Toolbar;
|
14 | 18 | import android.view.View;
|
15 | 19 |
|
| 20 | +import java.io.File; |
| 21 | +import java.io.IOException; |
16 | 22 | import java.util.Date;
|
17 | 23 |
|
18 | 24 | import butterknife.BindView;
|
@@ -140,8 +146,48 @@ public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference prefere
|
140 | 146 | getActivity().finish();
|
141 | 147 | }
|
142 | 148 | }
|
| 149 | + if (key.equals(getString(R.string.pref_key__is_show_in_gallery))) { |
| 150 | + boolean showInGallery = settings.getDefaultPreferences().getBoolean(key, true); |
| 151 | + File memeDirectory = AssetUpdater.getMemesDir(AppSettings.get()); |
| 152 | + File noMediaFile = new File(memeDirectory, ".nomedia"); |
| 153 | + if (showInGallery) { |
| 154 | + noMediaFile.delete(); |
| 155 | + context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(memeDirectory, ".nomedia")))); |
| 156 | + File[] files = memeDirectory.listFiles(); |
| 157 | + for (int i = 0; i < files.length; i++) { |
| 158 | + deleteFileFromMediaStore(context.getContentResolver(), files[i]); |
| 159 | + context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(files[i]))); |
| 160 | + } |
| 161 | + } else { |
| 162 | + try { |
| 163 | + noMediaFile.createNewFile(); |
| 164 | + context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(noMediaFile))); |
| 165 | + } catch (IOException e) { |
| 166 | + e.printStackTrace(); |
| 167 | + } |
| 168 | + } |
| 169 | + } |
143 | 170 | }
|
144 | 171 | return super.onPreferenceTreeClick(screen, preference);
|
145 | 172 | }
|
146 | 173 | }
|
| 174 | + |
| 175 | + public static void deleteFileFromMediaStore(final ContentResolver contentResolver, final File file) { |
| 176 | + String canonicalPath; |
| 177 | + try { |
| 178 | + canonicalPath = file.getCanonicalPath(); |
| 179 | + } catch (IOException e) { |
| 180 | + canonicalPath = file.getAbsolutePath(); |
| 181 | + } |
| 182 | + final Uri uri = MediaStore.Files.getContentUri("external"); |
| 183 | + final int result = contentResolver.delete(uri, |
| 184 | + MediaStore.Files.FileColumns.DATA + "=?", new String[]{canonicalPath}); |
| 185 | + if (result == 0) { |
| 186 | + final String absolutePath = file.getAbsolutePath(); |
| 187 | + if (!absolutePath.equals(canonicalPath)) { |
| 188 | + contentResolver.delete(uri, |
| 189 | + MediaStore.Files.FileColumns.DATA + "=?", new String[]{absolutePath}); |
| 190 | + } |
| 191 | + } |
| 192 | + } |
147 | 193 | }
|
0 commit comments