ManageSpaceActivity.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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.os.AsyncTask;
  22. import android.os.Bundle;
  23. import android.view.MenuItem;
  24. import android.view.View;
  25. import android.widget.Button;
  26. import android.widget.TextView;
  27. import com.google.android.material.snackbar.Snackbar;
  28. import com.nextcloud.client.preferences.AppPreferences;
  29. import com.nextcloud.client.preferences.PreferenceManager;
  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. private AppPreferences preferences;
  39. @Override
  40. protected void onCreate(Bundle savedInstanceState) {
  41. super.onCreate(savedInstanceState);
  42. setContentView(R.layout.activity_manage_space);
  43. ActionBar actionBar = getSupportActionBar();
  44. actionBar.setDisplayHomeAsUpEnabled(true);
  45. actionBar.setTitle(R.string.manage_space_title);
  46. TextView descriptionTextView = findViewById(R.id.general_description);
  47. descriptionTextView.setText(getString(R.string.manage_space_description, getString(R.string.app_name)));
  48. Button clearDataButton = findViewById(R.id.clearDataButton);
  49. clearDataButton.setOnClickListener(new View.OnClickListener() {
  50. @Override
  51. public void onClick(View v) {
  52. ClearDataAsyncTask clearDataTask = new ClearDataAsyncTask();
  53. clearDataTask.execute();
  54. }
  55. });
  56. preferences = PreferenceManager.fromContext(this);
  57. }
  58. @Override
  59. public boolean onOptionsItemSelected(MenuItem item) {
  60. boolean retval = true;
  61. switch (item.getItemId()) {
  62. case android.R.id.home:
  63. finish();
  64. break;
  65. default:
  66. Log_OC.w(TAG, "Unknown menu item triggered");
  67. retval = super.onOptionsItemSelected(item);
  68. break;
  69. }
  70. return retval;
  71. }
  72. /**
  73. * AsyncTask for Clear Data, saving the passcode
  74. */
  75. private class ClearDataAsyncTask extends AsyncTask<Void, Void, Boolean>{
  76. private final AppPreferences preferences = ManageSpaceActivity.this.preferences;
  77. @Override
  78. protected Boolean doInBackground(Void... params) {
  79. String lockPref = preferences.getLockPreference();
  80. boolean passCodeEnable = SettingsActivity.LOCK_PASSCODE.equals(lockPref);
  81. String passCodeDigits[] = new String[4];
  82. if (passCodeEnable) {
  83. passCodeDigits = preferences.getPassCode();
  84. }
  85. // Clear data
  86. preferences.clear();
  87. boolean result = clearApplicationData();
  88. // Recover passcode
  89. if (passCodeEnable) {
  90. preferences.setPassCode(
  91. passCodeDigits[0],
  92. passCodeDigits[1],
  93. passCodeDigits[2],
  94. passCodeDigits[3]
  95. );
  96. }
  97. preferences.setLockPreference(lockPref);
  98. return result;
  99. }
  100. @Override
  101. protected void onPostExecute(Boolean result) {
  102. super.onPostExecute(result);
  103. if (!result) {
  104. Snackbar.make(
  105. findViewById(android.R.id.content),
  106. R.string.manage_space_clear_data,
  107. Snackbar.LENGTH_LONG
  108. ).show();
  109. } else {
  110. finish();
  111. System.exit(0);
  112. }
  113. }
  114. public boolean clearApplicationData() {
  115. boolean clearResult = true;
  116. File appDir = new File(getCacheDir().getParent());
  117. if (appDir.exists()) {
  118. String[] children = appDir.list();
  119. if (children != null) {
  120. for (String s : children) {
  121. if (!LIB_FOLDER.equals(s)) {
  122. File fileToDelete = new File(appDir, s);
  123. clearResult = clearResult && deleteDir(fileToDelete);
  124. Log_OC.d(TAG, "Clear Application Data, File: " + fileToDelete.getName() + " DELETED *****");
  125. }
  126. }
  127. } else {
  128. clearResult = false;
  129. }
  130. }
  131. return clearResult;
  132. }
  133. public boolean deleteDir(File dir) {
  134. if (dir != null && dir.isDirectory()) {
  135. String[] children = dir.list();
  136. if (children != null) {
  137. for (String child : children) {
  138. boolean success = deleteDir(new File(dir, child));
  139. if (!success) {
  140. Log_OC.w(TAG, "File NOT deleted " + child);
  141. return false;
  142. } else {
  143. Log_OC.d(TAG, "File deleted " + child);
  144. }
  145. }
  146. } else {
  147. return false;
  148. }
  149. }
  150. if (dir != null) {
  151. return dir.delete();
  152. } else {
  153. return false;
  154. }
  155. }
  156. }
  157. }