Prepare preferences and project to use user defined storage path
authortobiasKaminsky <tobias@kaminsky.me>
Fri, 9 Oct 2015 15:20:47 +0000 (17:20 +0200)
committerBartosz Przybylski <bart.p.pl@gmail.com>
Sun, 15 Nov 2015 19:34:33 +0000 (20:34 +0100)
res/values/strings.xml
res/xml/preferences.xml
src/com/owncloud/android/MainApp.java
src/com/owncloud/android/ui/activity/Preferences.java
src/com/owncloud/android/utils/FileStorageUtils.java

index 320eea1..0dcca4c 100644 (file)
     <string name="file_list__footer__files">%1$d files</string>
     <string name="file_list__footer__files_and_folder">%1$d files, 1 folder</string>
     <string name="file_list__footer__files_and_folders">%1$d files, %2$d folders</string>
+    <string name="prefs_storage_path">Storage path</string>
+    <string name="prefs_common">Common</string>
 
     <string name="share_dialog_title">Sharing</string>
     <string name="share_with_user_section_title">Share with Users and Groups</string>
index 4823a83..18ab5b0 100644 (file)
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 -->
 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
+       <PreferenceCategory android:title="@string/prefs_common">
+               <EditTextPreference
+                       android:title="@string/prefs_storage_path"
+                       android:key="storage_path" />
+       </PreferenceCategory>
     <PreferenceCategory android:title="@string/prefs_category_accounts" android:key="accounts_category">
     </PreferenceCategory>
     
index c805f9f..4f0ee62 100644 (file)
@@ -23,10 +23,13 @@ package com.owncloud.android;
 import android.app.Activity;
 import android.app.Application;
 import android.content.Context;
+import android.content.SharedPreferences;
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
 import android.os.Build;
 import android.os.Bundle;
+import android.os.Environment;
+import android.preference.PreferenceManager;
 
 import com.owncloud.android.authentication.PassCodeManager;
 import com.owncloud.android.datamodel.ThumbnailsCacheManager;
@@ -54,6 +57,8 @@ public class MainApp extends Application {
 
     private static Context mContext;
 
+    private static String storagePath;
+
     // TODO Enable when "On Device" is recovered?
     // TODO better place
     // private static boolean mOnlyOnDevice = false;
@@ -62,7 +67,12 @@ public class MainApp extends Application {
     public void onCreate(){
         super.onCreate();
         MainApp.mContext = getApplicationContext();
-        
+
+        SharedPreferences appPrefs =
+                PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
+        MainApp.storagePath = appPrefs.getString("storage_path", Environment.
+                              getExternalStorageDirectory().getAbsolutePath());
+
         boolean isSamlAuth = AUTH_ON.equals(getString(R.string.auth_method_saml_web_sso));
 
         OwnCloudClientManagerFactory.setUserAgent(getUserAgent());
@@ -135,6 +145,14 @@ public class MainApp extends Application {
         return MainApp.mContext;
     }
 
+    public static String getStoragePath(){
+        return MainApp.storagePath;
+    }
+
+    public static void setStoragePath(String path){
+        MainApp.storagePath = path;
+    }
+
     // Methods to obtain Strings referring app_name 
     //   From AccountAuthenticator 
     //   public static final String ACCOUNT_TYPE = "owncloud";    
index e1c5c10..6310532 100644 (file)
@@ -35,6 +35,7 @@ import android.content.pm.PackageManager.NameNotFoundException;
 import android.content.res.Configuration;
 import android.net.Uri;
 import android.os.Bundle;
+import android.os.Environment;
 import android.os.Handler;
 import android.os.IBinder;
 import android.preference.CheckBoxPreference;
@@ -76,6 +77,7 @@ import com.owncloud.android.files.services.FileDownloader;
 import com.owncloud.android.files.services.FileUploader;
 import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.services.OperationsService;
+import com.owncloud.android.ui.PreferenceWithLongSummary;
 import com.owncloud.android.ui.RadioButtonPreference;
 import com.owncloud.android.utils.DisplayUtils;
 
@@ -116,6 +118,8 @@ public class Preferences extends PreferenceActivity
     protected FileDownloader.FileDownloaderBinder mDownloaderBinder = null;
     protected FileUploader.FileUploaderBinder mUploaderBinder = null;
     private ServiceConnection mDownloadServiceConnection, mUploadServiceConnection = null;
+    private PreferenceWithLongSummary mPrefStoragePath;
+    private String mStoragePath;
 
     @SuppressWarnings("deprecation")
     @Override
@@ -363,6 +367,32 @@ public class Preferences extends PreferenceActivity
             }
         }
 
+        mPrefStoragePath =  (PreferenceWithLongSummary)findPreference("storage_path");
+        if (mPrefStoragePath != null){
+
+                mPrefStoragePath.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
+                    @Override
+                    public boolean onPreferenceChange(Preference preference, Object newValue) {
+                        MainApp.setStoragePath((String) newValue);
+                        return true;
+                    }
+                });
+
+//            mPrefStoragePath.setOnPreferenceClickListener(new OnPreferenceClickListener() {
+//                @Override
+//                public boolean onPreferenceClick(Preference preference) {
+//
+////                    if (!mUploadPath.endsWith(OCFile.PATH_SEPARATOR)) {
+////                        mUploadPath += OCFile.PATH_SEPARATOR;
+////                    }
+////                    Intent intent = new Intent(Preferences.this, UploadPathActivity.class);
+////                    intent.putExtra(UploadPathActivity.KEY_INSTANT_UPLOAD_PATH, mUploadPath);
+////                    startActivityForResult(intent, ACTION_SELECT_UPLOAD_PATH);
+////                    return true;
+//                }
+//            });
+        }
+
         mPrefInstantUploadPath =  findPreference("instant_upload_path");
         if (mPrefInstantUploadPath != null){
 
@@ -436,6 +466,7 @@ public class Preferences extends PreferenceActivity
        }
 
        loadInstantUploadPath();
+       loadStoragePath();
        loadInstantUploadVideoPath();
 
         /* ComponentsGetter */
@@ -788,6 +819,17 @@ public class Preferences extends PreferenceActivity
     }
 
     /**
+     * Load storage path set on preferences
+     */
+    private void loadStoragePath() {
+        SharedPreferences appPrefs =
+                PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
+        mStoragePath = appPrefs.getString("storage_path", Environment.getExternalStorageDirectory()
+                                                         .getAbsolutePath());
+        mPrefStoragePath.setSummary(mStoragePath);
+    }
+
+    /**
      * Save the "Instant Upload Path" on preferences
      */
     private void saveInstantUploadPathOnPreferences() {
@@ -802,10 +844,7 @@ public class Preferences extends PreferenceActivity
      * Load upload video path set on preferences
      */
     private void loadInstantUploadVideoPath() {
-        SharedPreferences appPrefs =
-                PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
-        mUploadVideoPath = appPrefs.getString("instant_video_upload_path", getString(R.string.instant_upload_path));
-        mPrefInstantVideoUploadPath.setSummary(mUploadVideoPath);
+        mPrefInstantVideoUploadPath.setSummary(MainApp.getStoragePath());
     }
 
     /**
index 1740ca5..6fcf285 100644 (file)
@@ -55,8 +55,9 @@ public class FileStorageUtils {
 
     
     public static final String getSavePath(String accountName) {
-        File sdCard = Environment.getExternalStorageDirectory();
-        return sdCard.getAbsolutePath() + "/" + MainApp.getDataFolder() + "/" + Uri.encode(accountName, "@");
+//        File sdCard = Environment.getExternalStorageDirectory();
+
+        return MainApp.getStoragePath() + "/" + MainApp.getDataFolder() + "/" + Uri.encode(accountName, "@");
         // URL encoding is an 'easy fix' to overcome that NTFS and FAT32 don't allow ":" in file names, that can be in the accountName since 0.1.190B
     }