Skip to content

Commit

Permalink
More asynchronous, add Snackbar and optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
javiersantos committed Jun 30, 2015
1 parent 6add0cc commit c5d433c
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 155 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.javiersantos.mlmanager.activities;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
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 @@ -24,15 +24,14 @@
import com.javiersantos.mlmanager.AppInfo;
import com.javiersantos.mlmanager.MLManagerApplication;
import com.javiersantos.mlmanager.R;
import com.javiersantos.mlmanager.async.DeleteDataInBackground;
import com.javiersantos.mlmanager.async.ExtractFileInBackground;
import com.javiersantos.mlmanager.utils.AppPreferences;
import com.javiersantos.mlmanager.utils.RootUtils;
import com.javiersantos.mlmanager.utils.UtilsRoot;
import com.javiersantos.mlmanager.utils.UtilsApp;
import com.javiersantos.mlmanager.utils.UtilsDialog;
import com.javiersantos.mlmanager.utils.UtilsUI;

import java.io.File;


public class AppActivity extends AppCompatActivity {
// Load Settings
private AppPreferences appPreferences;
Expand Down Expand Up @@ -93,7 +92,7 @@ private void setScreenElements() {
CardView start = (CardView) findViewById(R.id.start_card);
CardView extract = (CardView) findViewById(R.id.extract_card);
CardView uninstall = (CardView) findViewById(R.id.uninstall_card);
CardView cache = (CardView) findViewById(R.id.cache_card);
final CardView cache = (CardView) findViewById(R.id.cache_card);
CardView clearData = (CardView) findViewById(R.id.clear_data_card);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

Expand Down Expand Up @@ -143,25 +142,23 @@ public void onClick(View view) {
extract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
UtilsApp.copyFile(context, appInfo);
UtilsDialog.showSavedDialog(context, appInfo).show();
} catch (Exception e) {
e.printStackTrace();
}
MaterialDialog dialog = UtilsDialog.showTitleContentWithProgress(context
, String.format(getResources().getString(R.string.dialog_saving), appInfo.getName())
, getResources().getString(R.string.dialog_saving_description));
new ExtractFileInBackground((Activity) context, context, dialog, appInfo).execute();
}
});

if(RootUtils.isRooted()) {
if(UtilsRoot.isRooted()) {
cache.setVisibility(View.VISIBLE);
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)
, getResources().getString(R.string.dialog_cache_deleting_description));
new DeleteDataInBackground(dialog, appInfo.getData() + "/cache/**"
, getResources().getString(R.string.dialog_cache_success_title)
new DeleteDataInBackground(context, dialog, appInfo.getData() + "/cache/**"
, getResources().getString(R.string.dialog_cache_success)
, getResources().getString(R.string.dialog_cache_success_description, appInfo.getName())).execute();
}
});
Expand All @@ -170,10 +167,10 @@ public void onClick(View view) {
@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)
, getResources().getString(R.string.dialog_clear_data_deleting_description));
new DeleteDataInBackground(dialog, appInfo.getData() + "/**"
, getResources().getString(R.string.dialog_clear_data_success_title)
new DeleteDataInBackground(context, dialog, appInfo.getData() + "/**"
, getResources().getString(R.string.dialog_clear_data_success)
, getResources().getString(R.string.dialog_clear_data_success_description, appInfo.getName())).execute();
}
});
Expand All @@ -186,8 +183,8 @@ public void onClick(View view) {
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
File file = UtilsApp.copyFile(context, appInfo);
Intent shareIntent = UtilsApp.getShareIntent(file);
UtilsApp.copyFile(appInfo);
Intent shareIntent = UtilsApp.getShareIntent(UtilsApp.getOutputFilename(appInfo));
startActivity(Intent.createChooser(shareIntent, String.format(getResources().getString(R.string.send_to), appInfo.getName())));
}
});
Expand Down Expand Up @@ -238,34 +235,4 @@ 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 @@ -15,17 +15,18 @@
import android.widget.ImageView;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
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;
import com.javiersantos.mlmanager.activities.MainActivity;
import com.javiersantos.mlmanager.async.ExtractFileInBackground;
import com.javiersantos.mlmanager.utils.AppPreferences;
import com.javiersantos.mlmanager.utils.UtilsApp;
import com.javiersantos.mlmanager.utils.UtilsDialog;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -71,19 +72,17 @@ private void setButtonEvents(AppViewHolder appViewHolder, final AppInfo appInfo)
appExtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
UtilsApp.copyFile(context, appInfo);
UtilsDialog.showSavedDialog(context, appInfo).show();
} catch (Exception e) {
e.printStackTrace();
}
MaterialDialog dialog = UtilsDialog.showTitleContentWithProgress(context
, String.format(context.getResources().getString(R.string.dialog_saving), appInfo.getName())
, context.getResources().getString(R.string.dialog_saving_description));
new ExtractFileInBackground((Activity) context, context, dialog, appInfo).execute();
}
});
appShare.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
File file = UtilsApp.copyFile(context, appInfo);
Intent shareIntent = UtilsApp.getShareIntent(file);
UtilsApp.copyFile(appInfo);
Intent shareIntent = UtilsApp.getShareIntent(UtilsApp.getOutputFilename(appInfo));
context.startActivity(Intent.createChooser(shareIntent, String.format(context.getResources().getString(R.string.send_to), appInfo.getName())));
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.javiersantos.mlmanager.async;

import android.content.Context;
import android.os.AsyncTask;

import com.afollestad.materialdialogs.MaterialDialog;
import com.javiersantos.mlmanager.R;
import com.javiersantos.mlmanager.utils.UtilsDialog;
import com.javiersantos.mlmanager.utils.UtilsRoot;

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

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

@Override
protected Boolean doInBackground(Void... voids) {
boolean status = UtilsRoot.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), context.getResources().getString(R.string.dialog_cache_and_data_error_description));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.javiersantos.mlmanager.async;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;

import com.afollestad.materialdialogs.MaterialDialog;
import com.javiersantos.mlmanager.AppInfo;
import com.javiersantos.mlmanager.R;
import com.javiersantos.mlmanager.utils.UtilsApp;
import com.javiersantos.mlmanager.utils.UtilsDialog;

public class ExtractFileInBackground extends AsyncTask<Void, String, Boolean> {
private Context context;
private Activity activity;
private MaterialDialog dialog;
private AppInfo appInfo;

public ExtractFileInBackground(Activity activity, Context context, MaterialDialog dialog, AppInfo appInfo) {
this.activity = activity;
this.context = context;
this.dialog = dialog;
this.appInfo = appInfo;
}

@Override
protected Boolean doInBackground(Void... voids) {
Boolean status = UtilsApp.copyFile(appInfo);
return status;
}

@Override
protected void onPostExecute(Boolean status) {
super.onPostExecute(status);
dialog.dismiss();
if (status) {
UtilsDialog.showExtractedSnackbar(activity, String.format(context.getResources().getString(R.string.dialog_saved_description), appInfo.getName(), UtilsApp.getAPKFilename(appInfo)), context.getResources().getString(R.string.button_undo), UtilsApp.getOutputFilename(appInfo)).show();
} else {
UtilsDialog.showTitleContent(context, context.getResources().getString(R.string.dialog_extract_fail), context.getResources().getString(R.string.dialog_extract_fail_description));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public class AppPreferences {
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);
Expand All @@ -32,7 +31,7 @@ public int getRootStatus() {
}

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

Expand Down
48 changes: 27 additions & 21 deletions app/src/main/java/com/javiersantos/mlmanager/utils/UtilsApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;

import com.javiersantos.mlmanager.AppInfo;
import com.javiersantos.mlmanager.MLManagerApplication;
Expand All @@ -21,43 +22,48 @@ public static File getAppFolder() {
return new File(Environment.getExternalStorageDirectory() + "/MLManager");
}

public static File copyFile(Context context, AppInfo appInfo) {
appPreferences = MLManagerApplication.getAppPreferences();
public static Boolean copyFile(AppInfo appInfo) {
Boolean res = false;

File initialFile = new File(appInfo.getSource());
File finalFile;
File finalFile = getOutputFilename(appInfo);
Log.i("Initial: ", initialFile.toString());
Log.i("Source: ", finalFile.toString());

try {
FileUtils.copyFile(initialFile, finalFile);
res = true;
} catch (IOException e) {
e.printStackTrace();
}

return res;
}

public static String getAPKFilename(AppInfo appInfo) {
appPreferences = MLManagerApplication.getAppPreferences();
String res;

switch (appPreferences.getCustomFilename()) {
case "1":
finalFile = new File(getAppFolder().getPath() + "/" + appInfo.getAPK() + "_" + appInfo.getVersion() + ".apk");
res = appInfo.getAPK() + "_" + appInfo.getVersion();
break;
case "2":
finalFile = new File(getAppFolder().getPath() + "/" + appInfo.getName() + "_" + appInfo.getVersion() + ".apk");
res = appInfo.getName() + "_" + appInfo.getVersion();
break;
case "4":
finalFile = new File(getAppFolder().getPath() + "/" + appInfo.getName() + ".apk");
res = appInfo.getName();
break;
default:
finalFile = new File(getAppFolder().getPath() + "/" + appInfo.getAPK() + ".apk");
res = appInfo.getAPK();
break;
}

try {
FileUtils.copyFile(initialFile, finalFile);
} catch (IOException e) {
e.printStackTrace();
}

return finalFile;
return res;
}

public static Boolean existCacheFolder(String data) {
File f = new File(data + "/cache");
if (f.exists() && f.isDirectory()) {
return true;
} else {
return false;
}
public static File getOutputFilename(AppInfo appInfo) {
return new File(getAppFolder().getPath() + "/" + getAPKFilename(appInfo) + ".apk");
}

public static Boolean deleteAppFiles() {
Expand Down
Loading

0 comments on commit c5d433c

Please sign in to comment.