Skip to content

Commit

Permalink
Show termux main app instead of its sub-apps
Browse files Browse the repository at this point in the history
Closes #253
  • Loading branch information
emanuele-f committed Dec 25, 2023
1 parent ddec1a8 commit 6942b25
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions app/src/main/java/com/emanuelef/remote_capture/AppsLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.ArrayMap;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.collection.ArraySet;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.AsyncTaskLoader;
import androidx.loader.content.Loader;
Expand All @@ -44,6 +44,7 @@ public class AppsLoader implements LoaderManager.LoaderCallbacks<ArrayList<AppDe
private static final int OPERATION_LOAD_APPS_INFO = 23;
private AppsLoadListener mListener;
private final AppCompatActivity mContext;
private static final String TERMUX_PACKAGE = "com.termux";

public AppsLoader(AppCompatActivity context) {
mContext = context;
Expand All @@ -58,7 +59,7 @@ public AppsLoader setAppsLoadListener(AppsLoadListener listener) {
private ArrayList<AppDescriptor> asyncLoadAppsInfo() {
final PackageManager pm = mContext.getPackageManager();
ArrayList<AppDescriptor> apps = new ArrayList<>();
ArraySet<Integer> uids = new ArraySet<>();
ArrayMap<Integer, Integer> uid_to_pos = new ArrayMap<>();

Log.d(TAG, "Loading APPs...");
List<PackageInfo> packs = Utils.getInstalledPackages(pm, 0);
Expand All @@ -68,23 +69,39 @@ private ArrayList<AppDescriptor> asyncLoadAppsInfo() {
Log.d(TAG, "num apps (system+user): " + packs.size());
long tstart = Utils.now();

PackageInfo termuxPkgInfo = null;

// NOTE: a single uid can correspond to multiple packages, only take the first package found.
// The VPNService in android works with UID, so this choice is not restrictive.
for (int i = 0; i < packs.size(); i++) {
PackageInfo p = packs.get(i);
String package_name = p.applicationInfo.packageName;

if(!uids.contains(p.applicationInfo.uid) && !package_name.equals(app_package)) {
if (package_name.equals(TERMUX_PACKAGE))
termuxPkgInfo = p;

if(!uid_to_pos.containsKey(p.applicationInfo.uid) && !package_name.equals(app_package)) {
int uid = p.applicationInfo.uid;
AppDescriptor app = new AppDescriptor(pm, p);

uid_to_pos.put(uid, apps.size());
apps.add(app);
uids.add(uid);

//Log.d(TAG, appName + " - " + package_name + " [" + uid + "]" + (is_system ? " - SYS" : " - USR"));
}
}

if (termuxPkgInfo != null) {
// termux packages share the same UID. Use the main package if available. See #253
int uid = termuxPkgInfo.applicationInfo.uid;
Integer pos = uid_to_pos.get(uid);

if (pos != null) {
apps.remove(pos.intValue());
apps.add(new AppDescriptor(pm, termuxPkgInfo));
}
}

Collections.sort(apps);

Log.d(TAG, packs.size() + " apps loaded in " + (Utils.now() - tstart) +" seconds");
Expand Down

0 comments on commit 6942b25

Please sign in to comment.