Browse Source

Merge pull request #1461 from owncloud/manage_space_activity

Added own Activity to manage space instead of default "Clear data" button in device settings
David A. Velasco 9 năm trước cách đây
mục cha
commit
7931c321f2

+ 5 - 1
AndroidManifest.xml

@@ -45,7 +45,8 @@
         android:name=".MainApp"
         android:icon="@drawable/icon"
         android:label="@string/app_name"
-        android:theme="@style/Theme.ownCloud" >
+        android:theme="@style/Theme.ownCloud"
+        android:manageSpaceActivity=".ui.activity.ManageSpaceActivity">
         <activity
             android:name=".ui.activity.FileDisplayActivity"
             android:label="@string/app_name" >
@@ -202,6 +203,9 @@
             <meta-data android:name="android.app.searchable"
                        android:resource="@xml/users_and_groups_searchable"/>
         </activity>
+        <activity android:name=".ui.activity.ManageSpaceActivity"
+                  android:label="@string/manage_space_title"
+                  android:theme="@style/Theme.ownCloud" />
     </application>
 
 </manifest>

+ 46 - 0
res/layout/activity_manage_space.xml

@@ -0,0 +1,46 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ownCloud Android client application
+
+  Copyright (C) 2016 ownCloud Inc.
+
+  This program is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License version 2,
+  as published by the Free Software Foundation.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+-->
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+              android:orientation="vertical"
+              android:layout_width="match_parent"
+              android:layout_height="match_parent">
+
+    <TextView
+        android:id="@+id/general_description"
+        android:layout_height="wrap_content"
+        android:layout_width="match_parent"
+        android:layout_gravity="center_vertical"
+        android:singleLine="false"
+        android:layout_margin="@dimen/standard_margin"
+        style="?android:attr/editTextPreferenceStyle"
+        android:text="@string/manage_space_description"
+        />
+
+    <android.support.v7.widget.AppCompatButton
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:text="@string/manage_space_clear_data"
+        android:id="@+id/clearDataButton"
+        android:layout_gravity="center_horizontal"
+        android:layout_margin="@dimen/standard_margin"
+        android:theme="@style/Button.Primary"
+        style="@style/Button.Primary"
+        android:contentDescription="@string/manage_space_clear_data"/>
+
+</LinearLayout>

+ 5 - 0
res/values/strings.xml

@@ -404,4 +404,9 @@
 
     <string name="action_switch_grid_view">Grid view</string>
     <string name="action_switch_list_view">List view</string>
+
+    <string name="manage_space_title">Manage space</string>
+    <string name="manage_space_description">Settings, database and server certificates from %1$s\'s data will be deleted permanentlty. \n\nDownloaded files will be kept untouched.\n\nThis process can take some time.</string>
+    <string name="manage_space_clear_data">Clear data</string>
+    <string name="manage_space_error">Some files could not be deleted.</string>
 </resources>

+ 1 - 1
src/com/owncloud/android/authentication/PassCodeManager.java

@@ -109,7 +109,7 @@ public class PassCodeManager {
 
     private boolean passCodeIsEnabled() {
         SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(MainApp.getAppContext());
-        return (appPrefs.getBoolean("set_pincode", false));
+        return (appPrefs.getBoolean(PassCodeActivity.PREFERENCE_SET_PASSCODE, false));
     }
 
 }

+ 185 - 0
src/com/owncloud/android/ui/activity/ManageSpaceActivity.java

@@ -0,0 +1,185 @@
+/**
+ *   ownCloud Android client application
+ *
+ *   @author masensio
+ *   Copyright (C) 2016 ownCloud Inc.
+ *
+ *   This program is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License version 2,
+ *   as published by the Free Software Foundation.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+package com.owncloud.android.ui.activity;
+
+import android.content.SharedPreferences;
+import android.os.AsyncTask;
+import android.os.Bundle;
+import android.preference.PreferenceManager;
+import android.support.v7.app.ActionBar;
+import android.support.v7.app.AppCompatActivity;
+import android.view.MenuItem;
+import android.view.View;
+import android.widget.Button;
+import android.widget.TextView;
+import android.widget.Toast;
+
+import com.owncloud.android.R;
+import com.owncloud.android.lib.common.utils.Log_OC;
+
+import java.io.File;
+
+public class ManageSpaceActivity extends AppCompatActivity {
+
+    private static final String TAG = ManageSpaceActivity.class.getSimpleName();
+
+    private static final String LIB_FOLDER = "lib";
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.activity_manage_space);
+
+        ActionBar actionBar = getSupportActionBar();
+        actionBar.setDisplayHomeAsUpEnabled(true);
+        actionBar.setTitle(R.string.manage_space_title);
+
+        TextView descriptionTextView = (TextView) findViewById(R.id.general_description);
+        descriptionTextView.setText(getString(R.string.manage_space_description, getString(R.string.app_name)));
+
+        Button clearDataButton = (Button) findViewById(R.id.clearDataButton);
+        clearDataButton.setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                ClearDataAsynTask clearDataTask = new ClearDataAsynTask();
+                clearDataTask.execute();
+            }
+        });
+    }
+
+
+    @Override
+    public boolean onOptionsItemSelected(MenuItem item) {
+        super.onOptionsItemSelected(item);
+        switch (item.getItemId()) {
+            case android.R.id.home:
+                finish();
+                return true;
+            default:
+                Log_OC.w(TAG, "Unknown menu item triggered");
+                return false;
+        }
+    }
+
+    /**
+     * AsyncTask for Clear Data, saving the passcode
+     */
+    private class ClearDataAsynTask extends AsyncTask<Void, Void, Boolean>{
+
+        @Override
+        protected Boolean doInBackground(Void... params) {
+
+            boolean result = true;
+
+            // Save passcode from Share preferences
+            SharedPreferences appPrefs = PreferenceManager
+                    .getDefaultSharedPreferences(getApplicationContext());
+
+            boolean passCodeEnable = appPrefs.getBoolean(PassCodeActivity.PREFERENCE_SET_PASSCODE, false);
+
+            String passCodeDigits[] = new String[4];
+            if (passCodeEnable) {
+                passCodeDigits[0] = appPrefs.getString(PassCodeActivity.PREFERENCE_PASSCODE_D1, null);
+                passCodeDigits[1] = appPrefs.getString(PassCodeActivity.PREFERENCE_PASSCODE_D2, null);
+                passCodeDigits[2] = appPrefs.getString(PassCodeActivity.PREFERENCE_PASSCODE_D3, null);
+                passCodeDigits[3] = appPrefs.getString(PassCodeActivity.PREFERENCE_PASSCODE_D4, null);
+            }
+
+            // Clear data
+            result = clearApplicationData();
+
+
+            // Clear SharedPreferences
+            SharedPreferences.Editor appPrefsEditor = PreferenceManager
+                    .getDefaultSharedPreferences(getApplicationContext()).edit();
+            appPrefsEditor.clear();
+            result = result && appPrefsEditor.commit();
+
+            // Recover passcode
+            if (passCodeEnable) {
+                appPrefsEditor.putString(PassCodeActivity.PREFERENCE_PASSCODE_D1, passCodeDigits[0]);
+                appPrefsEditor.putString(PassCodeActivity.PREFERENCE_PASSCODE_D2, passCodeDigits[1]);
+                appPrefsEditor.putString(PassCodeActivity.PREFERENCE_PASSCODE_D3, passCodeDigits[2]);
+                appPrefsEditor.putString(PassCodeActivity.PREFERENCE_PASSCODE_D4, passCodeDigits[3]);
+            }
+
+            appPrefsEditor.putBoolean(PassCodeActivity.PREFERENCE_SET_PASSCODE, passCodeEnable);
+            result = result && appPrefsEditor.commit();
+
+            return result;
+        }
+
+        @Override
+        protected void onPostExecute(Boolean result) {
+            super.onPostExecute(result);
+            if (!result) {
+                Toast.makeText(getApplicationContext(),
+                        getString(R.string.manage_space_clear_data),
+                        Toast.LENGTH_LONG).show();
+            } else {
+                finish();
+                System.exit(0);
+            }
+
+        }
+
+        public boolean clearApplicationData() {
+            boolean clearResult = true;
+            File appDir = new File(getCacheDir().getParent());
+            if (appDir.exists()) {
+                String[] children = appDir.list();
+                if (children != null) {
+                    for (String s : children) {
+                        if (!LIB_FOLDER.equals(s)) {
+                            File fileToDelete = new File(appDir, s);
+                            clearResult = clearResult && deleteDir(fileToDelete);
+                            Log_OC.d(TAG, "Clear Application Data, File: " + fileToDelete.getName() + " DELETED *****");
+                        }
+                    }
+                } else {
+                    clearResult = false;
+                }
+            }
+            return  clearResult;
+        }
+
+        public boolean deleteDir(File dir) {
+            if (dir != null && dir.isDirectory()) {
+                String[] children = dir.list();
+                if (children != null) {
+                    for (int i = 0; i < children.length; i++) {
+                        boolean success = deleteDir(new File(dir, children[i]));
+                        if (!success) {
+                            Log_OC.w(TAG, "File NOT deleted " + children[i]);
+                            return false;
+                        } else {
+                            Log_OC.d(TAG, "File deleted " + children[i]);
+                        }
+                    }
+                } else {
+                    return false;
+                }
+            }
+
+            return dir.delete();
+        }
+    }
+}

+ 19 - 10
src/com/owncloud/android/ui/activity/PassCodeActivity.java

@@ -25,6 +25,7 @@ package com.owncloud.android.ui.activity;
 import java.util.Arrays;
 
 import android.content.Intent;
+
 import android.content.SharedPreferences;
 import android.os.Bundle;
 import android.preference.PreferenceManager;
@@ -42,6 +43,8 @@ import android.widget.Toast;
 import com.owncloud.android.R;
 import com.owncloud.android.lib.common.utils.Log_OC;
 
+import java.util.Arrays;
+
 public class PassCodeActivity extends AppCompatActivity {
 
     private static final String TAG = PassCodeActivity.class.getSimpleName();
@@ -53,6 +56,15 @@ public class PassCodeActivity extends AppCompatActivity {
     public final static String KEY_PASSCODE  = "KEY_PASSCODE";
     public final static String KEY_CHECK_RESULT = "KEY_CHECK_RESULT";
 
+    // NOTE: PREFERENCE_SET_PASSCODE must have the same value as preferences.xml-->android:key for passcode preference
+    public final static String PREFERENCE_SET_PASSCODE = "set_pincode";
+
+    public final static String PREFERENCE_PASSCODE_D = "PrefPinCode";
+    public final static String PREFERENCE_PASSCODE_D1 = "PrefPinCode1";
+    public final static String PREFERENCE_PASSCODE_D2 = "PrefPinCode2";
+    public final static String PREFERENCE_PASSCODE_D3 = "PrefPinCode3";
+    public final static String PREFERENCE_PASSCODE_D4 = "PrefPinCode4";
+
     private Button mBCancel;
     private TextView mPassCodeHdr;
     private TextView mPassCodeHdrExplanation;
@@ -297,7 +309,6 @@ public class PassCodeActivity extends AppCompatActivity {
 
         } else if (ACTION_CHECK_WITH_RESULT.equals(getIntent().getAction())) {
             if (checkPassCode()) {
-
                 Intent resultIntent = new Intent();
                 resultIntent.putExtra(KEY_CHECK_RESULT, true);
                 setResult(RESULT_OK, resultIntent);
@@ -358,10 +369,10 @@ public class PassCodeActivity extends AppCompatActivity {
             .getDefaultSharedPreferences(getApplicationContext());
 
         String savedPassCodeDigits[] = new String[4];
-        savedPassCodeDigits[0] = appPrefs.getString("PrefPinCode1", null);
-        savedPassCodeDigits[1] = appPrefs.getString("PrefPinCode2", null);
-        savedPassCodeDigits[2] = appPrefs.getString("PrefPinCode3", null);
-        savedPassCodeDigits[3] = appPrefs.getString("PrefPinCode4", null);
+        savedPassCodeDigits[0] = appPrefs.getString(PREFERENCE_PASSCODE_D1, null);
+        savedPassCodeDigits[1] = appPrefs.getString(PREFERENCE_PASSCODE_D2, null);
+        savedPassCodeDigits[2] = appPrefs.getString(PREFERENCE_PASSCODE_D3, null);
+        savedPassCodeDigits[3] = appPrefs.getString(PREFERENCE_PASSCODE_D4, null);
 
         boolean result = true;
         for (int i = 0; i < mPassCodeDigits.length && result; i++) {
@@ -422,14 +433,12 @@ public class PassCodeActivity extends AppCompatActivity {
      * Saves the pass code input by the user as the current pass code.
      */
     protected void savePassCodeAndExit() {
-        SharedPreferences.Editor appPrefs = PreferenceManager
-                .getDefaultSharedPreferences(getApplicationContext()).edit();
-
         Intent resultIntent = new Intent();
         resultIntent.putExtra(KEY_PASSCODE,
                 mPassCodeDigits[0] + mPassCodeDigits[1] + mPassCodeDigits[2] + mPassCodeDigits[3]);
 
         setResult(RESULT_OK, resultIntent);
+
         finish();
     }
 
@@ -444,8 +453,8 @@ public class PassCodeActivity extends AppCompatActivity {
         SharedPreferences appPrefs = PreferenceManager
                 .getDefaultSharedPreferences(getApplicationContext());
 
-        boolean state = appPrefs.getBoolean("set_pincode", false);
-        appPrefsE.putBoolean("set_pincode", !state);
+        boolean state = appPrefs.getBoolean(PREFERENCE_SET_PASSCODE, false);
+        appPrefsE.putBoolean(PREFERENCE_SET_PASSCODE, !state);
         // TODO WIP: this is reverting the value of the preference because it was changed BEFORE
         // entering
         // TODO         in this activity; was the PreferenceCheckBox in the caller who did it

+ 7 - 7
src/com/owncloud/android/ui/activity/Preferences.java

@@ -90,7 +90,7 @@ import com.owncloud.android.utils.DisplayUtils;
 public class Preferences extends PreferenceActivity
         implements AccountManagerCallback<Boolean>, ComponentsGetter {
     
-    private static final String TAG = "OwnCloudPreferences";
+    private static final String TAG = Preferences.class.getSimpleName();
 
     private static final int ACTION_SELECT_UPLOAD_PATH = 1;
     private static final int ACTION_SELECT_UPLOAD_VIDEO_PATH = 2;
@@ -223,8 +223,8 @@ public class Preferences extends PreferenceActivity
         // Register context menu for list of preferences.
         registerForContextMenu(getListView());
 
-        pCode = (CheckBoxPreference) findPreference("set_pincode");
-        if (pCode != null) {
+        pCode = (CheckBoxPreference) findPreference(PassCodeActivity.PREFERENCE_SET_PASSCODE);
+        if (pCode != null){
             pCode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
                 @Override
                 public boolean onPreferenceChange(Preference preference, Object newValue) {
@@ -547,7 +547,7 @@ public class Preferences extends PreferenceActivity
         super.onResume();
         SharedPreferences appPrefs =
                 PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
-        boolean state = appPrefs.getBoolean("set_pincode", false);
+        boolean state = appPrefs.getBoolean(PassCodeActivity.PREFERENCE_SET_PASSCODE, false);
         pCode.setChecked(state);
 
         // Populate the accounts category with the list of accounts
@@ -616,9 +616,9 @@ public class Preferences extends PreferenceActivity
                         .getDefaultSharedPreferences(getApplicationContext()).edit();
 
                 for (int i = 1; i <= 4; ++i) {
-                    appPrefs.putString("PrefPinCode" + i, passcode.substring(i-1, i));
+                    appPrefs.putString(PassCodeActivity.PREFERENCE_PASSCODE_D + i, passcode.substring(i-1, i));
                 }
-                appPrefs.putBoolean("set_pincode", true);
+                appPrefs.putBoolean(PassCodeActivity.PREFERENCE_SET_PASSCODE, true);
                 appPrefs.commit();
                 Toast.makeText(this, R.string.pass_code_stored, Toast.LENGTH_LONG).show();
             }
@@ -627,7 +627,7 @@ public class Preferences extends PreferenceActivity
 
                 SharedPreferences.Editor appPrefs = PreferenceManager
                         .getDefaultSharedPreferences(getApplicationContext()).edit();
-                appPrefs.putBoolean("set_pincode", false);
+                appPrefs.putBoolean(PassCodeActivity.PREFERENCE_SET_PASSCODE, false);
                 appPrefs.commit();
 
                 Toast.makeText(this, R.string.pass_code_removed, Toast.LENGTH_LONG).show();

+ 1 - 1
src/com/owncloud/android/ui/preview/PreviewImageFragment.java

@@ -233,7 +233,7 @@ public class PreviewImageFragment extends FileFragment {
     public void onPrepareOptionsMenu(Menu menu) {
         super.onPrepareOptionsMenu(menu);
 
-        if (mContainerActivity.getStorageManager() != null) {
+        if (mContainerActivity.getStorageManager() != null && getFile() != null) {
             // Update the file
             setFile(mContainerActivity.getStorageManager().getFileById(getFile().getFileId()));
 

+ 22 - 1
src/com/owncloud/android/ui/preview/PreviewTextFragment.java

@@ -1,3 +1,22 @@
+/**
+ *   ownCloud Android client application
+ *
+ *   Copyright (C) 2016 ownCloud Inc.
+ *
+ *   This program is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License version 2,
+ *   as published by the Free Software Foundation.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
 package com.owncloud.android.ui.preview;
 
 import android.accounts.Account;
@@ -382,8 +401,10 @@ public class PreviewTextFragment extends FileFragment {
     public void onStop() {
         super.onStop();
         Log_OC.e(TAG, "onStop");
-        if (mTextLoadTask != null)
+        if (mTextLoadTask != null) {
             mTextLoadTask.cancel(Boolean.TRUE);
+            mTextLoadTask.dismissLoadingDialog();
+        }
     }
 
     /**