Skip to content

Commit

Permalink
make getInstalledApps much faster
Browse files Browse the repository at this point in the history
  • Loading branch information
Veticia committed May 6, 2023
1 parent 4879aea commit 34fcc1e
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.veticia.piLauncherNext;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
Expand Down Expand Up @@ -62,6 +63,8 @@ public static synchronized SettingsProvider getInstance (Context context)
private Set<String> mSelectedGroups = new HashSet<>();
private Map<String, Long> mRecents = new HashMap<>();
Set<String> def = new HashSet<>();
public static final Map<String, Intent> launchIntents = new HashMap<>();
public static final Map<String, Long> installDates = new HashMap<>();

private SettingsProvider(Context context) {
mPreferences = context.getSharedPreferences(context.getPackageName() + "_preferences", Context.MODE_PRIVATE);
Expand Down Expand Up @@ -94,7 +97,6 @@ public void updateRecent(String packageName, Long timestamp) {
}

public ArrayList<ApplicationInfo> getInstalledApps(Context context, List<String> selected, boolean first) {

// Get list of installed apps
Map<String, String> apps = getAppList();
ArrayList<ApplicationInfo> installedApplications = new ArrayList<>();
Expand All @@ -107,7 +109,6 @@ public ArrayList<ApplicationInfo> getInstalledApps(Context context, List<String>
}
installedApplications.addAll(vrApps);
}

if (isPlatformEnabled(KEY_PLATFORM_ANDROID)) {
List<ApplicationInfo> androidApps = new AndroidPlatform().getInstalledApps(context);
for (ApplicationInfo app : androidApps) {
Expand All @@ -117,7 +118,6 @@ public ArrayList<ApplicationInfo> getInstalledApps(Context context, List<String>
}
installedApplications.addAll(androidApps);
}

if (isPlatformEnabled(KEY_PLATFORM_PSP)) {
// only add PSP apps if the platform is supported
List<ApplicationInfo> pspApps = new PSPPlatform().getInstalledApps(context);
Expand All @@ -128,7 +128,6 @@ public ArrayList<ApplicationInfo> getInstalledApps(Context context, List<String>
}
installedApplications.addAll(pspApps);
}

// Save changes to app list
setAppList(mAppList);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public static boolean isOculusHeadset() {

public static boolean isPicoHeadset() {
String vendor = Build.MANUFACTURER.toUpperCase();
return vendor.replace("İ","I").startsWith("PICO");
return vendor.startsWith("PICO") || vendor.startsWith("PİCO");
}

protected static boolean isVirtualRealityApp(ApplicationInfo app) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,35 @@
import android.content.pm.PackageManager;
import android.util.Log;

import com.veticia.piLauncherNext.SettingsProvider;

import java.util.ArrayList;

public class AndroidPlatform extends AbstractPlatform {
public ArrayList<ApplicationInfo> getInstalledApps(Context context) {
ArrayList<ApplicationInfo> installedApps = new ArrayList<>();
PackageManager pm = context.getPackageManager();
ArrayList<ApplicationInfo> output = new ArrayList<>();
for (ApplicationInfo app : pm.getInstalledApplications(PackageManager.GET_META_DATA)) {
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(app.packageName);
if (!isVirtualRealityApp(app) && launchIntent != null) {
try {
PackageInfo packageInfo = pm.getPackageInfo(app.packageName, 0);
long installDate = packageInfo.firstInstallTime;
app.taskAffinity = Long.toString(installDate);
if (!isVirtualRealityApp(app)) {
if (!SettingsProvider.launchIntents.containsKey(app.packageName)) {
SettingsProvider.launchIntents.put(app.packageName, pm.getLaunchIntentForPackage(app.packageName));
}
catch (PackageManager.NameNotFoundException e) {
app.taskAffinity = "0";
if(SettingsProvider.launchIntents.get(app.packageName) != null) {
if(!SettingsProvider.installDates.containsKey(app.packageName)) {
long installDate;
try {
PackageInfo packageInfo = pm.getPackageInfo(app.packageName, 0);
installDate = packageInfo.firstInstallTime;
} catch (PackageManager.NameNotFoundException e) {
throw new RuntimeException(e);
}
SettingsProvider.installDates.put(app.packageName, installDate);
}
installedApps.add(app);
}
output.add(app);
}
}
return output;
return installedApps;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.widget.ImageView;

import com.veticia.piLauncherNext.MainActivity;
import com.veticia.piLauncherNext.SettingsProvider;

import net.didion.loopy.iso9660.ISO9660FileEntry;
import net.didion.loopy.iso9660.ISO9660FileSystem;
Expand Down Expand Up @@ -38,12 +39,11 @@ public ArrayList<ApplicationInfo> getInstalledApps(Context context) {
ApplicationInfo app = new ApplicationInfo();
app.name = path.substring(path.lastIndexOf('/') + 1);
app.packageName = PACKAGE_PREFIX + path;
File file = new File(path);
long lastModDate = file.lastModified();
app.taskAffinity = Long.toString(lastModDate);
if(!SettingsProvider.installDates.containsKey(app.packageName)) {
File file = new File(path);
SettingsProvider.installDates.put(app.packageName, file.lastModified());
}
output.add(app);
//debug
//Log.e("PSPmDate", app.name + " @ " + app.taskAffinity);
}
return output;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import android.content.pm.PackageManager;
import android.util.Log;

import com.veticia.piLauncherNext.SettingsProvider;

import java.util.ArrayList;

public class VRPlatform extends AbstractPlatform {
Expand All @@ -15,20 +17,25 @@ public ArrayList<ApplicationInfo> getInstalledApps(Context context) {
if (!isSupported()) {
return installedApps;
}

PackageManager pm = context.getPackageManager();
for (ApplicationInfo app : pm.getInstalledApplications(PackageManager.GET_META_DATA)) {
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(app.packageName);
if (isVirtualRealityApp(app) && launchIntent != null) {
try {
PackageInfo packageInfo = pm.getPackageInfo(app.packageName, 0);
long installDate = packageInfo.firstInstallTime;
app.taskAffinity = Long.toString(installDate);
if (isVirtualRealityApp(app)) {
if (!SettingsProvider.launchIntents.containsKey(app.packageName)) {
SettingsProvider.launchIntents.put(app.packageName, pm.getLaunchIntentForPackage(app.packageName));
}
catch (PackageManager.NameNotFoundException e) {
app.taskAffinity = "0";
if(SettingsProvider.launchIntents.get(app.packageName) != null) {
if(!SettingsProvider.installDates.containsKey(app.packageName)) {
long installDate;
try {
PackageInfo packageInfo = pm.getPackageInfo(app.packageName, 0);
installDate = packageInfo.firstInstallTime;
} catch (PackageManager.NameNotFoundException e) {
throw new RuntimeException(e);
}
SettingsProvider.installDates.put(app.packageName, installDate);
}
installedApps.add(app);
}
installedApps.add(app);
}
}
return installedApps;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,21 +248,12 @@ public void onImageSelected(String path, ImageView selectedImageView) {
this.notifyDataSetChanged(); // for real time updates
}

@SuppressWarnings("RedundantVariableInitializer")
private Long getInstallDate(ApplicationInfo applicationInfo) {
String installDateString;
long installDateLong = 0L;
if (applicationInfo.taskAffinity != null) {
installDateString = applicationInfo.taskAffinity;
} else {
installDateString = "0";
}
try {
installDateLong = Long.parseLong(installDateString);
} catch (NumberFormatException e) {
// result 0 is already defined by default
if(SettingsProvider.installDates.containsKey(applicationInfo.packageName)) {
return SettingsProvider.installDates.get(applicationInfo.packageName);
}else{
return 0L;
}
return installDateLong;
}

public void sort(SORT_FIELD field, SORT_ORDER order) {
Expand Down

0 comments on commit 34fcc1e

Please sign in to comment.