mirror of
https://github.com/emanuele-f/PCAPdroid.git
synced 2026-07-03 21:21:12 +08:00
Rename AppDescriptor getters
This commit is contained in:
parent
8ccdd69263
commit
0b34620233
@ -47,7 +47,7 @@ public class ActionReceiver extends BroadcastReceiver {
|
||||
NotificationManagerCompat man = NotificationManagerCompat.from(context);
|
||||
man.cancel(CaptureService.NOTIFY_ID_APP_BLOCKED);
|
||||
|
||||
AppDescriptor app = AppsResolver.resolve(context.getPackageManager(), unblock_app, 0);
|
||||
AppDescriptor app = AppsResolver.resolveInstalledApp(context.getPackageManager(), unblock_app, 0);
|
||||
String label = (app != null) ? app.getName() : unblock_app;
|
||||
Utils.showToastLong(context, R.string.app_unblocked, label);
|
||||
}
|
||||
|
||||
@ -35,7 +35,6 @@ import androidx.core.content.ContextCompat;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class AppsResolver {
|
||||
private static final String TAG = "AppsResolver";
|
||||
@ -92,7 +91,9 @@ public class AppsResolver {
|
||||
virtualIconLoader,"nobody", 9999, true));
|
||||
}
|
||||
|
||||
public static AppDescriptor resolve(PackageManager pm, String packageName, int pm_flags) {
|
||||
// Get the AppDescriptor corresponding to the given package name
|
||||
// No caching occurs. Virtual apps cannot be used.
|
||||
public static AppDescriptor resolveInstalledApp(PackageManager pm, String packageName, int pm_flags) {
|
||||
PackageInfo pinfo;
|
||||
|
||||
try {
|
||||
@ -106,11 +107,12 @@ public class AppsResolver {
|
||||
}
|
||||
|
||||
@SuppressLint("DiscouragedPrivateApi")
|
||||
public @Nullable AppDescriptor get(int uid, int pm_flags) {
|
||||
public @Nullable AppDescriptor getAppByUid(int uid, int pm_flags) {
|
||||
AppDescriptor app = mApps.get(uid);
|
||||
if(app != null)
|
||||
return app;
|
||||
|
||||
// Map the uid to the package name(s)
|
||||
String[] packages = null;
|
||||
|
||||
try {
|
||||
@ -126,13 +128,17 @@ public class AppsResolver {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Since multiple packages names may be returned, sort them and get the first to provide a
|
||||
// consistent name
|
||||
Arrays.sort(packages);
|
||||
// Impose order to guarantee that a uid is always mapped to the same package name.
|
||||
//
|
||||
String packageName = packages[0];
|
||||
for(String pkg: packages) {
|
||||
if(pkg.compareTo(packageName) < 0)
|
||||
packageName = pkg;
|
||||
}
|
||||
|
||||
// In case of root capture, we may be capturing traffic of different users/work profiles.
|
||||
// To get the correct label and icon, try to resolve the app as the specific user of the connection.
|
||||
if(!mFallbackToGlobalResolution && CaptureService.isCapturingAsRoot()) {
|
||||
// Try to resolve for the specific user
|
||||
try {
|
||||
if(getPackageInfoAsUser == null)
|
||||
getPackageInfoAsUser = PackageManager.class.getDeclaredMethod("getPackageInfoAsUser", String.class, int.class, int.class);
|
||||
@ -148,7 +154,7 @@ public class AppsResolver {
|
||||
}
|
||||
|
||||
if(app == null)
|
||||
app = resolve(mPm, packageName, pm_flags);
|
||||
app = resolveInstalledApp(mPm, packageName, pm_flags);
|
||||
|
||||
if(app != null)
|
||||
mApps.put(uid, app);
|
||||
@ -156,16 +162,12 @@ public class AppsResolver {
|
||||
return app;
|
||||
}
|
||||
|
||||
public @Nullable AppDescriptor getByPackage(String package_name, int pm_flags) {
|
||||
public @Nullable AppDescriptor getAppByPackage(String package_name, int pm_flags) {
|
||||
int uid = getUid(package_name);
|
||||
if(uid == Utils.UID_NO_FILTER)
|
||||
return null;
|
||||
|
||||
return get(uid, pm_flags);
|
||||
}
|
||||
|
||||
public @Nullable AppDescriptor lookup(int uid) {
|
||||
return mApps.get(uid);
|
||||
return getAppByUid(uid, pm_flags);
|
||||
}
|
||||
|
||||
/* Lookup a UID by package name (including virtual apps).
|
||||
|
||||
@ -132,7 +132,7 @@ public class CaptureService extends VpnService implements Runnable {
|
||||
private NotificationCompat.Builder mMalwareBuilder;
|
||||
private long mMonitoredNetwork;
|
||||
private ConnectivityManager.NetworkCallback mNetworkCallback;
|
||||
private AppsResolver appsResolver;
|
||||
private AppsResolver nativeAppsResolver; // can only be accessed by native code to avoid concurrency issues
|
||||
private boolean mMalwareDetectionEnabled;
|
||||
private boolean mBlacklistsUpdateRequested;
|
||||
private boolean mFirewallEnabled;
|
||||
@ -193,7 +193,7 @@ public class CaptureService extends VpnService implements Runnable {
|
||||
@Override
|
||||
public void onCreate() {
|
||||
Log.d(CaptureService.TAG, "onCreate");
|
||||
appsResolver = new AppsResolver(this);
|
||||
nativeAppsResolver = new AppsResolver(this);
|
||||
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
mSettings = new CaptureSettings(mPrefs); // initialize to prevent NULL pointer exceptions in methods (e.g. isRootCapture)
|
||||
|
||||
@ -484,7 +484,7 @@ public class CaptureService extends VpnService implements Runnable {
|
||||
mBlocklist.save();
|
||||
reloadBlocklist();
|
||||
|
||||
AppDescriptor app = appsResolver.getByPackage(packageName, 0);
|
||||
AppDescriptor app = AppsResolver.resolveInstalledApp(getPackageManager(), packageName, 0);
|
||||
String label = (app != null) ? app.getName() : packageName;
|
||||
|
||||
PendingIntent pi = PendingIntent.getActivity(CaptureService.this, 0,
|
||||
@ -625,7 +625,8 @@ public class CaptureService extends VpnService implements Runnable {
|
||||
public void notifyBlacklistedConnection(ConnectionDescriptor conn) {
|
||||
int uid = conn.uid;
|
||||
|
||||
AppDescriptor app = appsResolver.get(conn.uid, 0);
|
||||
AppsResolver resolver = new AppsResolver(this);
|
||||
AppDescriptor app = resolver.getAppByUid(conn.uid, 0);
|
||||
if(app == null)
|
||||
return;
|
||||
|
||||
@ -1304,7 +1305,7 @@ public class CaptureService extends VpnService implements Runnable {
|
||||
|
||||
// NOTE: to be invoked only by the native code
|
||||
public String getApplicationByUid(int uid) {
|
||||
AppDescriptor dsc = appsResolver.get(uid, 0);
|
||||
AppDescriptor dsc = nativeAppsResolver.getAppByUid(uid, 0);
|
||||
|
||||
if(dsc == null)
|
||||
return "";
|
||||
|
||||
@ -198,7 +198,7 @@ public class ConnectionsRegister {
|
||||
conn.asn = mGeo.getASN(dstAddr);
|
||||
//Log.d(TAG, "IP geolocation: IP=" + conn.dst_ip + " -> country=" + conn.country + ", ASN: " + conn.asn);
|
||||
|
||||
AppDescriptor app = mAppsResolver.get(conn.uid, 0);
|
||||
AppDescriptor app = mAppsResolver.getAppByUid(conn.uid, 0);
|
||||
if(app != null)
|
||||
conn.encrypted_payload = Utils.hasEncryptedPayload(app, conn);
|
||||
|
||||
|
||||
@ -1256,7 +1256,7 @@ public class Utils {
|
||||
|
||||
public static int getPCAPdroidUid(Context context) {
|
||||
// NOTE: when called from a work profile, it correctly returns the work profile UID
|
||||
AppDescriptor app = AppsResolver.resolve(context.getPackageManager(), BuildConfig.APPLICATION_ID, 0);
|
||||
AppDescriptor app = AppsResolver.resolveInstalledApp(context.getPackageManager(), BuildConfig.APPLICATION_ID, 0);
|
||||
if(app != null)
|
||||
return app.getUid();
|
||||
return Utils.UID_UNKNOWN;
|
||||
|
||||
@ -152,7 +152,7 @@ public class CaptureCtrl extends AppCompatActivity {
|
||||
|
||||
private AppDescriptor getCallingApp() {
|
||||
String callp = getCallingPackage();
|
||||
return (callp != null) ? AppsResolver.resolve(getPackageManager(), callp, 0) : null;
|
||||
return (callp != null) ? AppsResolver.resolveInstalledApp(getPackageManager(), callp, 0) : null;
|
||||
}
|
||||
|
||||
private void controlAction(Intent intent, String action, boolean allow) {
|
||||
|
||||
@ -315,7 +315,7 @@ public class MainActivity extends BaseActivity implements NavigationView.OnNavig
|
||||
|
||||
final String peerAppPackage = "com.emanuelef.remote_capture";
|
||||
|
||||
AppDescriptor peer = AppsResolver.resolve(getPackageManager(), peerAppPackage, 0);
|
||||
AppDescriptor peer = AppsResolver.resolveInstalledApp(getPackageManager(), peerAppPackage, 0);
|
||||
if(peer == null) {
|
||||
Log.d(TAG, "Peer app not found");
|
||||
mIab.clearPeerSkus();
|
||||
|
||||
@ -77,7 +77,7 @@ public class AppsStatsAdapter extends RecyclerView.Adapter<AppsStatsAdapter.View
|
||||
Drawable appIcon;
|
||||
|
||||
// NOTE: can be null
|
||||
AppDescriptor app = (mApps != null) ? mApps.get(stats.getUid(), 0) : null;
|
||||
AppDescriptor app = (mApps != null) ? mApps.getAppByUid(stats.getUid(), 0) : null;
|
||||
|
||||
appIcon = ((app != null) && (app.getIcon() != null)) ? app.getIcon() : mUnknownIcon;
|
||||
icon.setImageDrawable(appIcon);
|
||||
@ -176,7 +176,7 @@ public class AppsStatsAdapter extends RecyclerView.Adapter<AppsStatsAdapter.View
|
||||
if(stats == null)
|
||||
return null;
|
||||
|
||||
AppDescriptor descr = mApps.get(stats.getUid(), 0);
|
||||
AppDescriptor descr = mApps.getAppByUid(stats.getUid(), 0);
|
||||
|
||||
return((descr != null) ? descr.getPackageName() : null);
|
||||
}
|
||||
@ -187,8 +187,8 @@ public class AppsStatsAdapter extends RecyclerView.Adapter<AppsStatsAdapter.View
|
||||
|
||||
public void setStats(List<AppStats> stats) {
|
||||
Collections.sort(stats, (o1, o2) -> {
|
||||
AppDescriptor a1 = mApps.get(o1.getUid(), 0);
|
||||
AppDescriptor a2 = mApps.get(o2.getUid(), 0);
|
||||
AppDescriptor a1 = mApps.getAppByUid(o1.getUid(), 0);
|
||||
AppDescriptor a2 = mApps.getAppByUid(o2.getUid(), 0);
|
||||
|
||||
if((a1 == null) && (a2 == null))
|
||||
return 0;
|
||||
|
||||
@ -58,7 +58,7 @@ public class ConnectionsAdapter extends RecyclerView.Adapter<ConnectionsAdapter.
|
||||
private final Drawable mUnknownIcon;
|
||||
private int mUnfilteredItemsCount;
|
||||
private View.OnClickListener mListener;
|
||||
private final AppsResolver mApps;
|
||||
private final AppsResolver mAppsResolver;
|
||||
private final Context mContext;
|
||||
private ConnectionDescriptor mSelectedItem;
|
||||
private int mNumRemovedItems;
|
||||
@ -109,7 +109,7 @@ public class ConnectionsAdapter extends RecyclerView.Adapter<ConnectionsAdapter.
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public void bindConn(Context context, ConnectionDescriptor conn, AppsResolver apps, Drawable unknownIcon) {
|
||||
AppDescriptor app = apps.get(conn.uid, 0);
|
||||
AppDescriptor app = apps.getAppByUid(conn.uid, 0);
|
||||
Drawable appIcon;
|
||||
String l7Text;
|
||||
|
||||
@ -168,7 +168,7 @@ public class ConnectionsAdapter extends RecyclerView.Adapter<ConnectionsAdapter.
|
||||
|
||||
public ConnectionsAdapter(Context context, AppsResolver resolver) {
|
||||
mContext = context;
|
||||
mApps = resolver;
|
||||
mAppsResolver = resolver;
|
||||
mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
mUnknownIcon = ContextCompat.getDrawable(context, R.drawable.ic_image);
|
||||
mListener = null;
|
||||
@ -216,7 +216,7 @@ public class ConnectionsAdapter extends RecyclerView.Adapter<ConnectionsAdapter.
|
||||
return;
|
||||
}
|
||||
|
||||
holder.bindConn(mContext, conn, mApps, mUnknownIcon);
|
||||
holder.bindConn(mContext, conn, mAppsResolver, mUnknownIcon);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -229,7 +229,7 @@ public class ConnectionsAdapter extends RecyclerView.Adapter<ConnectionsAdapter.
|
||||
private boolean matches(ConnectionDescriptor conn) {
|
||||
return((conn != null)
|
||||
&& mFilter.matches(conn)
|
||||
&& ((mSearch == null) || conn.matches(mApps, mSearch)));
|
||||
&& ((mSearch == null) || conn.matches(mAppsResolver, mSearch)));
|
||||
}
|
||||
|
||||
// Given an incrId, return the position of the connection into the mFilteredConn array
|
||||
@ -455,7 +455,7 @@ public class ConnectionsAdapter extends RecyclerView.Adapter<ConnectionsAdapter.
|
||||
ConnectionDescriptor conn = getItem(i);
|
||||
|
||||
if(conn != null) {
|
||||
AppDescriptor app = resolver.get(conn.uid, 0);
|
||||
AppDescriptor app = resolver.getAppByUid(conn.uid, 0);
|
||||
|
||||
builder.append(conn.ipproto); builder.append(",");
|
||||
builder.append(conn.src_ip); builder.append(",");
|
||||
|
||||
@ -63,7 +63,7 @@ public class CtrlPermissionsAdapter extends ArrayAdapter<CtrlPermissions.Rule> i
|
||||
|
||||
while(it.hasNext()) {
|
||||
CtrlPermissions.Rule rule = it.next();
|
||||
AppDescriptor app = AppsResolver.resolve(pm, rule.package_name, 0);
|
||||
AppDescriptor app = AppsResolver.resolveInstalledApp(pm, rule.package_name, 0);
|
||||
if(app != null)
|
||||
mPkgToApp.put(rule.package_name, app);
|
||||
|
||||
|
||||
@ -69,7 +69,7 @@ public class ListEditAdapter extends ArrayAdapter<MatchList.Rule> implements Tex
|
||||
|
||||
if(rule.getType() == MatchList.RuleType.APP) {
|
||||
String package_name = (String)rule.getValue();
|
||||
AppDescriptor app = mApps.getByPackage(package_name, 0);
|
||||
AppDescriptor app = mApps.getAppByPackage(package_name, 0);
|
||||
Drawable drawable = ((app != null) && (app.getIcon() != null)) ? app.getIcon() : mUnknownIcon;
|
||||
icon.setImageDrawable(drawable);
|
||||
} else
|
||||
|
||||
@ -88,7 +88,7 @@ public class AppOverview extends Fragment implements MenuProvider {
|
||||
Context ctx = requireContext();
|
||||
|
||||
AppsResolver res = new AppsResolver(ctx);
|
||||
AppDescriptor dsc = res.get(mUid, PackageManager.GET_PERMISSIONS);
|
||||
AppDescriptor dsc = res.getAppByUid(mUid, PackageManager.GET_PERMISSIONS);
|
||||
if(dsc == null) {
|
||||
mCreateError = true;
|
||||
Utils.showToast(ctx, R.string.app_not_found, mUid);
|
||||
|
||||
@ -177,7 +177,7 @@ public class ConnectionOverview extends Fragment implements ConnectionDetailsAct
|
||||
info_row.setVisibility(View.GONE);
|
||||
|
||||
String uid_str = Integer.toString(mConn.uid);
|
||||
AppDescriptor app = (new AppsResolver(mActivity)).get(mConn.uid, 0);
|
||||
AppDescriptor app = (new AppsResolver(mActivity)).getAppByUid(mConn.uid, 0);
|
||||
if(app != null)
|
||||
appLabel.setText(String.format(getResources().getString(R.string.app_and_proto), app.getName(), uid_str));
|
||||
else
|
||||
|
||||
@ -290,7 +290,7 @@ public class ConnectionsFragment extends Fragment implements ConnectionsListener
|
||||
if(conn == null)
|
||||
return;
|
||||
|
||||
AppDescriptor app = mApps.get(conn.uid, 0);
|
||||
AppDescriptor app = mApps.getAppByUid(conn.uid, 0);
|
||||
Context ctx = requireContext();
|
||||
MenuItem item;
|
||||
|
||||
@ -473,7 +473,7 @@ public class ConnectionsFragment extends Fragment implements ConnectionsListener
|
||||
mask_changed = true;
|
||||
} else if(id == R.id.search_app)
|
||||
setQuery(Objects.requireNonNull(
|
||||
mApps.get(conn.uid, 0)).getPackageName());
|
||||
mApps.getAppByUid(conn.uid, 0)).getPackageName());
|
||||
else if(id == R.id.search_host)
|
||||
setQuery(conn.info);
|
||||
else if(id == R.id.search_ip)
|
||||
|
||||
@ -220,7 +220,7 @@ public class StatusFragment extends Fragment implements AppStateListener, MenuPr
|
||||
|
||||
mAppFilterSwitch.setChecked(true);
|
||||
|
||||
AppDescriptor app = AppsResolver.resolve(context.getPackageManager(), mAppFilter, 0);
|
||||
AppDescriptor app = AppsResolver.resolveInstalledApp(context.getPackageManager(), mAppFilter, 0);
|
||||
String description;
|
||||
|
||||
if(app == null)
|
||||
@ -290,7 +290,7 @@ public class StatusFragment extends Fragment implements AppStateListener, MenuPr
|
||||
|
||||
// Check if a filter is set
|
||||
if((mAppFilter != null) && (!mAppFilter.isEmpty())) {
|
||||
AppDescriptor app = AppsResolver.resolve(requireContext().getPackageManager(), mAppFilter, 0);
|
||||
AppDescriptor app = AppsResolver.resolveInstalledApp(requireContext().getPackageManager(), mAppFilter, 0);
|
||||
|
||||
if((app != null) && (app.getIcon() != null)) {
|
||||
// Rendering after mCollectorInfo.setText is deferred, so getMeasuredHeight must be postponed
|
||||
|
||||
@ -20,7 +20,6 @@
|
||||
package com.emanuelef.remote_capture.model;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
@ -233,7 +232,7 @@ public class ConnectionDescriptor {
|
||||
|
||||
public boolean matches(AppsResolver res, String filter) {
|
||||
filter = filter.toLowerCase();
|
||||
AppDescriptor app = res.get(uid, 0);
|
||||
AppDescriptor app = res.getAppByUid(uid, 0);
|
||||
|
||||
return(((info != null) && (info.contains(filter))) ||
|
||||
dst_ip.contains(filter) ||
|
||||
|
||||
@ -159,7 +159,7 @@ public class MatchList {
|
||||
|
||||
if(tp == RuleType.APP) {
|
||||
// TODO handle cross-users/profiles?
|
||||
AppDescriptor app = AppsResolver.resolve(ctx.getPackageManager(), value, 0);
|
||||
AppDescriptor app = AppsResolver.resolveInstalledApp(ctx.getPackageManager(), value, 0);
|
||||
if(app != null)
|
||||
value = app.getName();
|
||||
} else if(tp == RuleType.HOST)
|
||||
@ -223,7 +223,7 @@ public class MatchList {
|
||||
try {
|
||||
int uid = Integer.parseInt(val);
|
||||
|
||||
AppDescriptor app = mResolver.get(uid, 0);
|
||||
AppDescriptor app = mResolver.getAppByUid(uid, 0);
|
||||
if(app != null) {
|
||||
val = app.getPackageName();
|
||||
Log.i(TAG, String.format("UID %d resolved to package %s", uid, val));
|
||||
@ -253,7 +253,7 @@ public class MatchList {
|
||||
public void addCountry(String country_code) { addRule(new Rule(RuleType.COUNTRY, country_code)); }
|
||||
public void addApp(String pkg) { addRule(new Rule(RuleType.APP, pkg)); }
|
||||
public void addApp(int uid) {
|
||||
AppDescriptor app = mResolver.get(uid, 0);
|
||||
AppDescriptor app = mResolver.getAppByUid(uid, 0);
|
||||
if(app == null) {
|
||||
Log.e(TAG, "could not resolve UID " + uid);
|
||||
return;
|
||||
@ -269,7 +269,7 @@ public class MatchList {
|
||||
public void removeCountry(String country_code) { removeRule(new Rule(RuleType.COUNTRY, country_code)); }
|
||||
public void removeApp(String pkg) { removeRule(new Rule(RuleType.APP, pkg)); }
|
||||
public void removeApp(int uid) {
|
||||
AppDescriptor app = mResolver.get(uid, 0);
|
||||
AppDescriptor app = mResolver.getAppByUid(uid, 0);
|
||||
if(app == null) {
|
||||
Log.e(TAG, "could not resolve UID " + uid);
|
||||
return;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user