ManageSpaceActivity.java 6.1 KB

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