From 96ce49707be83baec6ada86cd0f8ed04eba52669 Mon Sep 17 00:00:00 2001 From: masensio Date: Fri, 29 Jan 2016 13:46:22 +0100 Subject: [PATCH] Clean app data (keeping the PASSCODE untouched) from ManageSpaceActivity --- AndroidManifest.xml | 3 +- res/layout/activity_manage_space.xml | 22 ++++ res/values/strings.xml | 5 +- .../ui/activity/ManageSpaceActivity.java | 111 ++++++++++++++++++ 4 files changed, 139 insertions(+), 2 deletions(-) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 4b3faa76e5..1f37156fd2 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -204,7 +204,8 @@ android:resource="@xml/users_and_groups_searchable"/> + android:label="@string/manage_space_title" + android:theme="@style/Theme.ownCloud" /> diff --git a/res/layout/activity_manage_space.xml b/res/layout/activity_manage_space.xml index fb3d8a2580..b199488b14 100644 --- a/res/layout/activity_manage_space.xml +++ b/res/layout/activity_manage_space.xml @@ -4,4 +4,26 @@ android:layout_width="match_parent" android:layout_height="match_parent"> + + + + \ No newline at end of file diff --git a/res/values/strings.xml b/res/values/strings.xml index 14db630d8f..b5c478b1b2 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -405,5 +405,8 @@ Grid view List view - Manage space + Manage space + All %1$s\'s data will be deleted permanentlty. \nThis includes all files, settings, accounts, databases, etc. \n\nThis process can take some minutes. + Clear data + %1$s\'s data deleted diff --git a/src/com/owncloud/android/ui/activity/ManageSpaceActivity.java b/src/com/owncloud/android/ui/activity/ManageSpaceActivity.java index b235b9d7b1..186d28bf0c 100644 --- a/src/com/owncloud/android/ui/activity/ManageSpaceActivity.java +++ b/src/com/owncloud/android/ui/activity/ManageSpaceActivity.java @@ -1,17 +1,128 @@ package com.owncloud.android.ui.activity; +import android.content.Intent; +import android.content.SharedPreferences; 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(); + @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) { + clearData(); + } + }); + } + + /** + * Save passcode from Share preferences + * Clear the rest of data + */ + private void clearData() { + // Save passcode from Share preferences + SharedPreferences appPrefs = PreferenceManager + .getDefaultSharedPreferences(getApplicationContext()); + + boolean passCodeEnable = appPrefs.getBoolean("set_pincode", false); + + String passCodeDigits[] = new String[4]; + if (passCodeEnable) { + passCodeDigits[0] = appPrefs.getString("PrefPinCode1", null); + passCodeDigits[1] = appPrefs.getString("PrefPinCode2", null); + passCodeDigits[2] = appPrefs.getString("PrefPinCode3", null); + passCodeDigits[3] = appPrefs.getString("PrefPinCode4", null); + } + + // Clear data + clearApplicationData(); + + // Recover passcode + SharedPreferences.Editor appPrefsEditor = PreferenceManager + .getDefaultSharedPreferences(getApplicationContext()).edit(); + if (passCodeEnable) { + appPrefsEditor.putString("PrefPinCode1", passCodeDigits[0]); + appPrefsEditor.putString("PrefPinCode2", passCodeDigits[1]); + appPrefsEditor.putString("PrefPinCode3", passCodeDigits[2]); + appPrefsEditor.putString("PrefPinCode4", passCodeDigits[3]); + } + + appPrefsEditor.putBoolean("set_pincode", passCodeEnable); + appPrefsEditor.commit(); + + String message = getString(R.string.manage_string_message, getString(R.string.app_name)); + Toast.makeText(this, message, Toast.LENGTH_LONG).show(); + + } + + public void clearApplicationData() { + File cache = getCacheDir(); + File appDir = new File(cache.getParent()); + if (appDir.exists()) { + String[] children = appDir.list(); + for (String s : children) { + if (!s.equals("lib")) { + deleteDir(new File(appDir, s)); + Log_OC.i(TAG, "*******File /data/data/" + getString(R.string.app_name)+ + "/" + s + " DELETED *******"); + } + } + } + } + + public static boolean deleteDir(File dir) { + if (dir != null && dir.isDirectory()) { + String[] children = dir.list(); + for (int i = 0; i < children.length; i++) { + boolean success = deleteDir(new File(dir, children[i])); + if (!success) { + return false; + } + } + } + + return dir.delete(); } + @Override + public boolean onOptionsItemSelected(MenuItem item) { + super.onOptionsItemSelected(item); + switch (item.getItemId()) { + case android.R.id.home: + finish(); + break; + default: + Log_OC.w(TAG, "Unknown menu item triggered"); + return false; + } + return true; + } }