Changes from comments in PR Code Review

This commit is contained in:
masensio 2016-02-03 13:30:40 +01:00
parent 9e83827cc4
commit 14530aa7b8
6 changed files with 166 additions and 94 deletions

View file

@ -1,4 +1,21 @@
<?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"
@ -10,7 +27,7 @@
android:layout_width="match_parent"
android:layout_gravity="center_vertical"
android:singleLine="false"
android:layout_margin="10dp"
android:layout_margin="@dimen/standard_margin"
style="?android:attr/editTextPreferenceStyle"
android:text="@string/manage_space_description"
/>
@ -21,7 +38,7 @@
android:text="@string/manage_space_clear_data"
android:id="@+id/clearDataButton"
android:layout_gravity="center_horizontal"
android:layout_margin="4dp"
android:layout_margin="@dimen/standard_half_margin"
android:theme="@style/Button.Primary"
style="@style/Button.Primary"
android:contentDescription="@string/manage_space_clear_data"/>

View file

@ -408,4 +408,5 @@
<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 keep untouched.\n\nThis process can take some time.</string>
<string name="manage_space_clear_data">Clear data</string>
<string name="manage_space_error">Some error occurs when trying to clean app data.</string>
</resources>

View file

@ -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));
}
}

View file

@ -1,6 +1,27 @@
/**
* 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;
@ -9,6 +30,7 @@ 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;
@ -19,6 +41,8 @@ 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);
@ -35,85 +59,12 @@ public class ManageSpaceActivity extends AppCompatActivity {
clearDataButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clearData();
ClearDataAsynTask clearDataTask = new ClearDataAsynTask();
clearDataTask.execute();
}
});
}
/**
* 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();
// Clear SharedPreferences
SharedPreferences.Editor appPrefsEditor = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext()).edit();
appPrefsEditor.clear();
appPrefsEditor.commit();
// Recover passcode
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();
finish();
System.exit(0);
}
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")) {
File fileToDelete = new File(appDir, s);
deleteDir(fileToDelete);
Log_OC.d(TAG, "Clear Application Data, File: " + fileToDelete.getName()+ " DELETED *******");
}
}
}
}
public 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) {
@ -121,11 +72,106 @@ public class ManageSpaceActivity extends AppCompatActivity {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
return true;
default:
Log_OC.w(TAG, "Unknown menu item triggered");
return false;
}
return true;
}
/**
* 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();
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 *******");
}
}
}
return clearResult;
}
public 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) {
Log_OC.d(TAG, "File NOT deleted " + children[i]);
return false;
} else {
Log_OC.d(TAG, "File deleted " + children[i]);
}
}
}
return dir.delete();
}
}
}

View file

@ -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,14 @@ 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_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 +308,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 +368,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 +432,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 +452,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

View file

@ -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