ManageSpaceActivity.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author masensio
  5. * Copyright (C) 2016 ownCloud Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package com.owncloud.android.ui.activity;
  21. import android.content.SharedPreferences;
  22. import android.os.AsyncTask;
  23. import android.os.Bundle;
  24. import android.preference.PreferenceManager;
  25. import android.view.MenuItem;
  26. import android.view.View;
  27. import android.widget.Button;
  28. import android.widget.TextView;
  29. import com.google.android.material.snackbar.Snackbar;
  30. import com.owncloud.android.R;
  31. import com.owncloud.android.lib.common.utils.Log_OC;
  32. import java.io.File;
  33. import androidx.appcompat.app.ActionBar;
  34. import androidx.appcompat.app.AppCompatActivity;
  35. public class ManageSpaceActivity extends AppCompatActivity {
  36. private static final String TAG = ManageSpaceActivity.class.getSimpleName();
  37. private static final String LIB_FOLDER = "lib";
  38. @Override
  39. protected void onCreate(Bundle savedInstanceState) {
  40. super.onCreate(savedInstanceState);
  41. setContentView(R.layout.activity_manage_space);
  42. ActionBar actionBar = getSupportActionBar();
  43. actionBar.setDisplayHomeAsUpEnabled(true);
  44. actionBar.setTitle(R.string.manage_space_title);
  45. TextView descriptionTextView = findViewById(R.id.general_description);
  46. descriptionTextView.setText(getString(R.string.manage_space_description, getString(R.string.app_name)));
  47. Button clearDataButton = findViewById(R.id.clearDataButton);
  48. clearDataButton.setOnClickListener(new View.OnClickListener() {
  49. @Override
  50. public void onClick(View v) {
  51. ClearDataAsynTask clearDataTask = new ClearDataAsynTask();
  52. clearDataTask.execute();
  53. }
  54. });
  55. }
  56. @Override
  57. public boolean onOptionsItemSelected(MenuItem item) {
  58. boolean retval = true;
  59. switch (item.getItemId()) {
  60. case android.R.id.home:
  61. finish();
  62. break;
  63. default:
  64. Log_OC.w(TAG, "Unknown menu item triggered");
  65. retval = super.onOptionsItemSelected(item);
  66. break;
  67. }
  68. return retval;
  69. }
  70. /**
  71. * AsyncTask for Clear Data, saving the passcode
  72. */
  73. private class ClearDataAsynTask extends AsyncTask<Void, Void, Boolean>{
  74. @Override
  75. protected Boolean doInBackground(Void... params) {
  76. // Save passcode from Share preferences
  77. SharedPreferences appPrefs = PreferenceManager
  78. .getDefaultSharedPreferences(getApplicationContext());
  79. String lockPref = appPrefs.getString(Preferences.PREFERENCE_LOCK, Preferences.LOCK_NONE);
  80. boolean passCodeEnable = Preferences.LOCK_PASSCODE.equals(
  81. appPrefs.getString(Preferences.PREFERENCE_LOCK, ""));
  82. String passCodeDigits[] = new String[4];
  83. if (passCodeEnable) {
  84. passCodeDigits[0] = appPrefs.getString(PassCodeActivity.PREFERENCE_PASSCODE_D1, null);
  85. passCodeDigits[1] = appPrefs.getString(PassCodeActivity.PREFERENCE_PASSCODE_D2, null);
  86. passCodeDigits[2] = appPrefs.getString(PassCodeActivity.PREFERENCE_PASSCODE_D3, null);
  87. passCodeDigits[3] = appPrefs.getString(PassCodeActivity.PREFERENCE_PASSCODE_D4, null);
  88. }
  89. // Clear data
  90. boolean result = clearApplicationData();
  91. // Clear SharedPreferences
  92. SharedPreferences.Editor appPrefsEditor = PreferenceManager
  93. .getDefaultSharedPreferences(getApplicationContext()).edit();
  94. appPrefsEditor.clear();
  95. result = result && appPrefsEditor.commit();
  96. // Recover passcode
  97. if (passCodeEnable) {
  98. appPrefsEditor.putString(PassCodeActivity.PREFERENCE_PASSCODE_D1, passCodeDigits[0]);
  99. appPrefsEditor.putString(PassCodeActivity.PREFERENCE_PASSCODE_D2, passCodeDigits[1]);
  100. appPrefsEditor.putString(PassCodeActivity.PREFERENCE_PASSCODE_D3, passCodeDigits[2]);
  101. appPrefsEditor.putString(PassCodeActivity.PREFERENCE_PASSCODE_D4, passCodeDigits[3]);
  102. }
  103. appPrefsEditor.putString(Preferences.PREFERENCE_LOCK, lockPref);
  104. result = result && appPrefsEditor.commit();
  105. return result;
  106. }
  107. @Override
  108. protected void onPostExecute(Boolean result) {
  109. super.onPostExecute(result);
  110. if (!result) {
  111. Snackbar.make(
  112. findViewById(android.R.id.content),
  113. R.string.manage_space_clear_data,
  114. Snackbar.LENGTH_LONG
  115. ).show();
  116. } else {
  117. finish();
  118. System.exit(0);
  119. }
  120. }
  121. public boolean clearApplicationData() {
  122. boolean clearResult = true;
  123. File appDir = new File(getCacheDir().getParent());
  124. if (appDir.exists()) {
  125. String[] children = appDir.list();
  126. if (children != null) {
  127. for (String s : children) {
  128. if (!LIB_FOLDER.equals(s)) {
  129. File fileToDelete = new File(appDir, s);
  130. clearResult = clearResult && deleteDir(fileToDelete);
  131. Log_OC.d(TAG, "Clear Application Data, File: " + fileToDelete.getName() + " DELETED *****");
  132. }
  133. }
  134. } else {
  135. clearResult = false;
  136. }
  137. }
  138. return clearResult;
  139. }
  140. public boolean deleteDir(File dir) {
  141. if (dir != null && dir.isDirectory()) {
  142. String[] children = dir.list();
  143. if (children != null) {
  144. for (String child : children) {
  145. boolean success = deleteDir(new File(dir, child));
  146. if (!success) {
  147. Log_OC.w(TAG, "File NOT deleted " + child);
  148. return false;
  149. } else {
  150. Log_OC.d(TAG, "File deleted " + child);
  151. }
  152. }
  153. } else {
  154. return false;
  155. }
  156. }
  157. if (dir != null) {
  158. return dir.delete();
  159. } else {
  160. return false;
  161. }
  162. }
  163. }
  164. }