Created
January 24, 2019 17:04
-
-
Save Binary-Finery/ae1176416c4a634d59007d2a93e43275 to your computer and use it in GitHub Desktop.
utility class for multi app uninstaller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.spencerstudios.multiappuninstaller.utilities; | |
import android.content.ActivityNotFoundException; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.pm.ApplicationInfo; | |
import android.content.pm.PackageInfo; | |
import android.net.Uri; | |
import android.widget.Toast; | |
import com.spencerstudios.multiappuninstaller.pojos.AppMeta; | |
import com.spencerstudios.multiappuninstaller.constants.Const; | |
import com.spencerstudios.multiappuninstaller.pojos.History; | |
import java.io.File; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.Comparator; | |
import java.util.List; | |
import java.util.Locale; | |
import spencerstudios.com.bungeelib.Bungee; | |
import spencerstudios.com.jetdblib.JetDB; | |
public class Utils { | |
private static boolean isSystemPackage(PackageInfo pkgInfo) { | |
return (pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0; | |
} | |
public static List<AppMeta> getInstalledApps(Context context) { | |
List<AppMeta> res = new ArrayList<>(); | |
List<PackageInfo> packs = context.getPackageManager().getInstalledPackages(0); | |
for (int i = 0; i < packs.size(); i++) { | |
PackageInfo packageInfo = packs.get(i); | |
if ((!isSystemPackage(packageInfo))) { | |
res.add(new AppMeta(packageInfo.applicationInfo.loadLabel(context.getPackageManager()).toString(), packageInfo.packageName, packageInfo.applicationInfo.loadIcon(context.getPackageManager()), packageInfo.firstInstallTime, getApkSize(packageInfo.applicationInfo.publicSourceDir), false)); | |
} | |
} | |
int sortType = PrefUtils.getSortType(context); | |
if (sortType == Const.SORT_BY_NAME) { | |
Collections.sort(res, new Comparator<AppMeta>() { | |
@Override | |
public int compare(AppMeta a, AppMeta b) { | |
return a.getAppName().compareTo(b.getAppName()); | |
} | |
}); | |
} else if (sortType == Const.SORT_BY_DATE) { | |
Collections.sort(res, new Comparator<AppMeta>() { | |
@Override | |
public int compare(AppMeta a, AppMeta b) { | |
return Long.valueOf(b.getFti()).compareTo(a.getFti()); | |
} | |
}); | |
} else if (sortType == Const.SORT_BY_NAME_DESCENDING) { | |
Collections.sort(res, new Comparator<AppMeta>() { | |
@Override | |
public int compare(AppMeta a, AppMeta b) { | |
return b.getAppName().compareTo(a.getAppName()); | |
} | |
}); | |
} else if (sortType == Const.SORT_BY_DATE_OLDEST) { | |
Collections.sort(res, new Comparator<AppMeta>() { | |
@Override | |
public int compare(AppMeta a, AppMeta b) { | |
return Long.valueOf(a.getFti()).compareTo(b.getFti()); | |
} | |
}); | |
} else if (sortType == Const.SORT_BY_SIZE_ASCENDING) { | |
Collections.sort(res, new Comparator<AppMeta>() { | |
@Override | |
public int compare(AppMeta a, AppMeta b) { | |
return Long.valueOf(a.getApkSize()).compareTo(b.getApkSize()); | |
} | |
}); | |
} else { | |
Collections.sort(res, new Comparator<AppMeta>() { | |
@Override | |
public int compare(AppMeta a, AppMeta b) { | |
return Long.valueOf(b.getApkSize()).compareTo(a.getApkSize()); | |
} | |
}); | |
} | |
return res; | |
} | |
private static long getApkSize(String apkPath) { | |
File apk = new File(apkPath); | |
return apk.length(); | |
} | |
public static String bytesToMb(long bytes) { | |
return String.format(Locale.getDefault(), "%.2f MB", ((double) bytes / 1048576)); | |
} | |
public static void addAppToHistory(Context context, History history) { | |
List<History> historyArrayList = JetDB.getListOfObjects(context, History.class, "history_db"); | |
historyArrayList.add(0, history); | |
saveHistory(context, historyArrayList); | |
} | |
public static List<History> getHistory(Context context) { | |
return JetDB.getListOfObjects(context, History.class, Const.HISTORY_DATA_BASE_KEY); | |
} | |
public static void saveHistory(Context context, List<History> temp) { | |
JetDB.putListOfObjects(context, temp, Const.HISTORY_DATA_BASE_KEY); | |
} | |
public static void clearHistoryList(Context context) { | |
List<History> emptyList = new ArrayList<>(); | |
saveHistory(context, emptyList); | |
} | |
public static void openInPlayStore(Context context, String packageName) { | |
try { | |
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(Const.PLAY_STORE_LINK.concat(packageName)))); | |
Bungee.split(context); | |
} catch (ActivityNotFoundException e) { | |
e.printStackTrace(); | |
Toast.makeText(context, "unable to open this apps Play Store link", Toast.LENGTH_LONG).show(); | |
} | |
} | |
public static void removeItemFromHistory(Context context, long datetime) { | |
List<History> temp = getHistory(context); | |
for (int i = 0; i < temp.size(); i++) { | |
if (temp.get(i).getUninstallDate() == datetime) { | |
temp.remove(i); | |
break; | |
} | |
} | |
saveHistory(context, temp); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment