Skip to content

Commit

Permalink
Added 2 features for rooted phones : - Clear cache and - Clear data. …
Browse files Browse the repository at this point in the history
…MLManagerApplication is created to provide single instace of AppPreferences. Earlier multiple instances were being created which is not required. RootUtils added for checking some root related info. Tested on a few rooted and unrooted phones.
  • Loading branch information
Vijay Rawat committed Jun 21, 2015
1 parent af335db commit e6e176c
Show file tree
Hide file tree
Showing 14 changed files with 260 additions and 18 deletions.
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/MLManager" >
android:theme="@style/MLManager"
android:name=".MLManagerApplication">
<activity
android:name=".activities.MainActivity"
android:label="@string/app_name" >
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.javiersantos.mlmanager;

import android.app.Application;

import com.javiersantos.mlmanager.utils.AppPreferences;

/**
* Created by vijay.rawat01 on 6/21/15.
*/
public class MLManagerApplication extends Application {

private static AppPreferences sAppPreferences;

@Override
public void onCreate() {
sAppPreferences = new AppPreferences(this);
super.onCreate();
}

public static AppPreferences getAppPreferences() {
return sAppPreferences;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.view.WindowManager;
import android.widget.TextView;

import com.javiersantos.mlmanager.MLManagerApplication;
import com.javiersantos.mlmanager.R;
import com.javiersantos.mlmanager.utils.AppPreferences;
import com.javiersantos.mlmanager.utils.UtilsApp;
Expand All @@ -22,7 +23,7 @@ public class AboutActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
this.appPreferences = new AppPreferences(getApplicationContext());
this.appPreferences = MLManagerApplication.getAppPreferences();

setInitialConfiguration();
setScreenElements();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
Expand All @@ -18,10 +19,13 @@
import android.widget.ImageView;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.getbase.floatingactionbutton.FloatingActionButton;
import com.javiersantos.mlmanager.AppInfo;
import com.javiersantos.mlmanager.MLManagerApplication;
import com.javiersantos.mlmanager.R;
import com.javiersantos.mlmanager.utils.AppPreferences;
import com.javiersantos.mlmanager.utils.RootUtils;
import com.javiersantos.mlmanager.utils.UtilsApp;
import com.javiersantos.mlmanager.utils.UtilsDialog;
import com.javiersantos.mlmanager.utils.UtilsUI;
Expand All @@ -45,7 +49,7 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app);
this.context = this;
this.appPreferences = new AppPreferences(getApplicationContext());
this.appPreferences = MLManagerApplication.getAppPreferences();

getInitialConfiguration();
setInitialConfiguration();
Expand Down Expand Up @@ -90,6 +94,7 @@ private void setScreenElements() {
CardView extract = (CardView) findViewById(R.id.extract_card);
CardView uninstall = (CardView) findViewById(R.id.uninstall_card);
CardView cache = (CardView) findViewById(R.id.cache_card);
CardView clearData = (CardView) findViewById(R.id.clear_data_card);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

icon.setImageDrawable(appInfo.getIcon());
Expand Down Expand Up @@ -147,14 +152,32 @@ public void onClick(View view) {
}
});

/*if (UtilsApp.existCacheFolder(appData)) {
if(RootUtils.isRooted()) {
cache.setVisibility(View.VISIBLE);
TextView cache_description = (TextView) findViewById(R.id.cache_description);
cache_description.setText(String.format(getString(R.string.dialog_cache_description), UtilsApp.getCacheFolderSize(appData)));
} else {
cache.setVisibility(View.GONE);
}*/
cache.setVisibility(View.GONE);
cache.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MaterialDialog dialog = UtilsDialog.showTitleContentWithProgress(context
, getResources().getString(R.string.dialog_cache_deleting_title)
, getResources().getString(R.string.dialog_cache_deleting_description));
new DeleteDataInBackground(dialog, appInfo.getData() + "/cache/**"
, getResources().getString(R.string.dialog_cache_success_title)
, getResources().getString(R.string.dialog_cache_success_description, appInfo.getName())).execute();
}
});
clearData.setVisibility(View.VISIBLE);
clearData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MaterialDialog dialog = UtilsDialog.showTitleContentWithProgress(context
, getResources().getString(R.string.dialog_clear_data_deleting_title)
, getResources().getString(R.string.dialog_clear_data_deleting_description));
new DeleteDataInBackground(dialog, appInfo.getData() + "/**"
, getResources().getString(R.string.dialog_clear_data_success_title)
, getResources().getString(R.string.dialog_clear_data_success_description, appInfo.getName())).execute();
}
});
}

// FAB
fab.setIcon(R.drawable.ic_send_white);
Expand Down Expand Up @@ -215,4 +238,34 @@ public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}

class DeleteDataInBackground extends AsyncTask<Void, String, Boolean> {
private MaterialDialog dialog;
private String directory;
private String successTitle;
private String successDescription;

public DeleteDataInBackground(MaterialDialog dialog, String directory, String successTitle, String successDescription) {
this.dialog = dialog;
this.directory = directory;
this.successTitle = successTitle;
this.successDescription = successDescription;
}

@Override
protected Boolean doInBackground(Void... voids) {
boolean status = RootUtils.removeWithRootPermission(directory);
return status;
}

@Override
protected void onPostExecute(Boolean status) {
super.onPostExecute(status);
dialog.dismiss();
if (status) {
UtilsDialog.showTitleContent(context, successTitle, successDescription);
} else {
UtilsDialog.showTitleContent(context, context.getResources().getString(R.string.dialog_cache_and_data_error_title), context.getResources().getString(R.string.dialog_cache_and_data_error_description));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import android.widget.Toast;

import com.javiersantos.mlmanager.AppInfo;
import com.javiersantos.mlmanager.MLManagerApplication;
import com.javiersantos.mlmanager.R;
import com.javiersantos.mlmanager.adapters.AppAdapter;
import com.javiersantos.mlmanager.utils.AppPreferences;
Expand Down Expand Up @@ -77,7 +78,7 @@ public class MainActivity extends AppCompatActivity implements SearchView.OnQuer
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.appPreferences = new AppPreferences(getApplicationContext());
this.appPreferences = MLManagerApplication.getAppPreferences();
this.context = this;

setInitialConfiguration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.javiersantos.mlmanager.MLManagerApplication;
import com.javiersantos.mlmanager.R;
import com.javiersantos.mlmanager.utils.AppPreferences;
import com.javiersantos.mlmanager.utils.UtilsApp;
Expand All @@ -42,7 +43,7 @@ public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
this.context = this;
this.appPreferences = new AppPreferences(getApplicationContext());
this.appPreferences = MLManagerApplication.getAppPreferences();

prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.registerOnSharedPreferenceChangeListener(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import android.widget.TextView;

import com.gc.materialdesign.views.ButtonFlat;
import com.javiersantos.mlmanager.MLManagerApplication;
import com.javiersantos.mlmanager.activities.AppActivity;
import com.javiersantos.mlmanager.AppInfo;
import com.javiersantos.mlmanager.R;
Expand All @@ -40,7 +41,7 @@ public class AppAdapter extends RecyclerView.Adapter<AppAdapter.AppViewHolder> i
public AppAdapter(List<AppInfo> appList, Context context) {
this.appList = appList;
this.context = context;
this.appPreferences = new AppPreferences(context);
this.appPreferences = MLManagerApplication.getAppPreferences();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,24 @@ public class AppPreferences {
public static final String KeyNavigationBlack = "prefNavigationBlack";
public static final String KeyCustomFilename = "prefCustomFilename";
public static final String KeySortMode = "prefSortMode";
public static final String KeyIsRooted = "prefIsRooted";
public static final String KeyIsRootedCheckDone = "prefIsRootedCheckDone";

public AppPreferences(Context context) {
this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
this.editor = sharedPreferences.edit();
this.context = context;
}

public int getRootStatus() {
return sharedPreferences.getInt(KeyIsRooted, 0);
}

public void setRootStatus(int rootStatus) {
editor.putInt(KeyStartDelete, rootStatus);
editor.commit();
}

public Boolean getStartDeletePref() {
return sharedPreferences.getBoolean(KeyStartDelete, false);
}
Expand Down
81 changes: 81 additions & 0 deletions app/src/main/java/com/javiersantos/mlmanager/utils/RootUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.javiersantos.mlmanager.utils;

import android.os.Build;

import com.javiersantos.mlmanager.MLManagerApplication;

import java.io.File;

/**
* Created by vijay.rawat01 on 6/21/15.
*/
public class RootUtils {

private static final int ROOT_STATUS_NOT_CHECKED = 0;
private static final int ROOT_STATUS_ROOTED = 1;
private static final int ROOT_STATUS_NOT_ROOTED = 2;

private RootUtils() {
}

public static boolean isRooted() {
int rootStatus = MLManagerApplication.getAppPreferences().getRootStatus();
boolean isRooted = false;
if (rootStatus == ROOT_STATUS_NOT_CHECKED) {
isRooted = isRootByBuildTag() || isRootedByFileSU() || isRootedByExecutingCommand();
MLManagerApplication.getAppPreferences().setRootStatus(isRooted ? ROOT_STATUS_ROOTED : ROOT_STATUS_NOT_ROOTED);
} else if (rootStatus == ROOT_STATUS_ROOTED) {
isRooted = true;
}
return isRooted;
}

public static boolean isRootByBuildTag() {
String buildTags = Build.TAGS;
return ((buildTags != null && buildTags.contains("test-keys")));
}

public static boolean isRootedByFileSU() {
try {
File file = new File("/system/app/Superuser.apk");
if (file.exists()) {
return true;
}
} catch (Exception e1) {
}
return false;
}

public static boolean isRootedByExecutingCommand() {
return canExecuteCommand("/system/xbin/which su")
|| canExecuteCommand("/system/bin/which su")
|| canExecuteCommand("which su");
}

public static boolean removeWithRootPermission(String directory) {
boolean status = false;
try {
String[] command = new String[]{"su", "-c", "rm -rf " + directory};
Process process = Runtime.getRuntime().exec(command);
process.waitFor();
int i = process.exitValue();
if (i == 0) {
status = true;
}
} catch (Exception e) {
}
return status;
}

private static boolean canExecuteCommand(String command) {
boolean isExecuted;
try {
Runtime.getRuntime().exec(command);
isExecuted = true;
} catch (Exception e) {
isExecuted = false;
}

return isExecuted;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.os.Environment;

import com.javiersantos.mlmanager.AppInfo;
import com.javiersantos.mlmanager.MLManagerApplication;

import org.apache.commons.io.FileUtils;

Expand All @@ -21,7 +22,7 @@ public static File getAppFolder() {
}

public static File copyFile(Context context, AppInfo appInfo) {
appPreferences = new AppPreferences(context);
appPreferences = MLManagerApplication.getAppPreferences();

File initialFile = new File(appInfo.getSource());
File finalFile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@

import com.afollestad.materialdialogs.MaterialDialog;
import com.javiersantos.mlmanager.AppInfo;
import com.javiersantos.mlmanager.MLManagerApplication;
import com.javiersantos.mlmanager.R;

public class UtilsDialog {
private static AppPreferences appPreferences;

public static MaterialDialog.Builder showSavedDialog(Context context, AppInfo appInfo) {
appPreferences = new AppPreferences(context);
appPreferences = MLManagerApplication.getAppPreferences();
String filename;

switch (appPreferences.getCustomFilename()) {
Expand Down Expand Up @@ -70,4 +71,25 @@ public static MaterialDialog.Builder showIndeterminateProgressDialog(Context con
return materialBuilder;
}

public static MaterialDialog showTitleContent(Context context, String title, String content) {
MaterialDialog.Builder materialBuilder = new MaterialDialog.Builder(context)
.title(title)
.content(content).positiveText(context.getResources().getString(R.string.button_ok)).cancelable(true).callback(new MaterialDialog.ButtonCallback() {
@Override
public void onPositive(MaterialDialog dialog) {
dialog.dismiss();
}
});
return materialBuilder.show();
}

public static MaterialDialog showTitleContentWithProgress(Context context, String title, String content) {
MaterialDialog.Builder materialBuilder = new MaterialDialog.Builder(context)
.title(title)
.content(content)
.cancelable(false)
.progress(true, 0);
return materialBuilder.show();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.view.View;
import android.widget.AdapterView;

import com.javiersantos.mlmanager.MLManagerApplication;
import com.javiersantos.mlmanager.activities.AboutActivity;
import com.javiersantos.mlmanager.R;
import com.javiersantos.mlmanager.activities.SettingsActivity;
Expand Down Expand Up @@ -40,7 +41,7 @@ public static int darker (int color, double factor) {

public static Drawer setNavigationDrawer (Activity activity, final Context context, Toolbar toolbar, final AppAdapter appAdapter, final AppAdapter appSystemAdapter, final RecyclerView recyclerView) {
int header;
appPreferences = new AppPreferences(context);
appPreferences = MLManagerApplication.getAppPreferences();
String apps, systemApps;

if (getDayOrNight() == 1) {
Expand Down
Loading

0 comments on commit e6e176c

Please sign in to comment.