|
| 1 | +package org.sofwerx.torgi; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.content.SharedPreferences; |
| 5 | +import android.os.Environment; |
| 6 | +import android.preference.PreferenceManager; |
| 7 | + |
| 8 | +import java.io.File; |
| 9 | + |
| 10 | +public class Config { |
| 11 | + public final static String PREFS_SAVE_DIR = "savedir"; |
| 12 | + |
| 13 | + private static Config instance = null; |
| 14 | + private String savedDir = null; |
| 15 | + private SharedPreferences prefs = null; |
| 16 | + private Context context; |
| 17 | + |
| 18 | + private Config(Context context) { |
| 19 | + this.context = context; |
| 20 | + prefs = PreferenceManager.getDefaultSharedPreferences(context); |
| 21 | + } |
| 22 | + |
| 23 | + public static Config getInstance(Context context) { |
| 24 | + if (instance == null) |
| 25 | + instance = new Config(context); |
| 26 | + return instance; |
| 27 | + } |
| 28 | + |
| 29 | + public String getSavedDir() { |
| 30 | + if (savedDir == null) { |
| 31 | + /*savedDir = prefs.getString(PREFS_SAVE_DIR, null); |
| 32 | + if (savedDir != null) { |
| 33 | + try { |
| 34 | + Uri savedDirUri = Uri.parse(savedDir); |
| 35 | + if (savedDirUri != null) { |
| 36 | + context.getContentResolver().takePersistableUriPermission(savedDirUri, |
| 37 | + Intent.FLAG_GRANT_READ_URI_PERMISSION | |
| 38 | + Intent.FLAG_GRANT_WRITE_URI_PERMISSION); //Keep the permissions to access this location up to date across reboots |
| 39 | + } |
| 40 | + } catch (NullPointerException ignore) {} |
| 41 | + } |
| 42 | + if (savedDir == null) |
| 43 | + savedDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();*/ |
| 44 | + if (savedDir == null) { |
| 45 | + File folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "TORGI"); |
| 46 | + folder.mkdirs(); |
| 47 | + savedDir = folder.getAbsolutePath(); |
| 48 | + } |
| 49 | + } |
| 50 | + return savedDir; |
| 51 | + } |
| 52 | + |
| 53 | + public void setSavedDir(String savedDir) { |
| 54 | + SharedPreferences.Editor edit = prefs.edit(); |
| 55 | + if (savedDir == null) |
| 56 | + edit.remove(PREFS_SAVE_DIR); |
| 57 | + else |
| 58 | + edit.putString(PREFS_SAVE_DIR,savedDir); |
| 59 | + edit.commit(); |
| 60 | + } |
| 61 | +} |
0 commit comments