Skip to content

Commit

Permalink
when activities started or destroyed,count it for their package,inste…
Browse files Browse the repository at this point in the history
…ad of counting pid for package
  • Loading branch information
Saint-Theana authored and Saint-Theana committed Oct 23, 2021
1 parent 90464f0 commit 64635f0
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 140 deletions.
162 changes: 45 additions & 117 deletions app/src/me/piebridge/prevent/framework/ActivityReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ abstract class ActivityReceiver extends BroadcastReceiver {
protected final HashMap<String, String> audioFocusedActivity = new HashMap<String, String>();
private final Map<String, Integer> packageUids = new HashMap<String, Integer>();
private final Map<String, Set<String>> abnormalProcesses = new ConcurrentHashMap<String, Set<String>>();
private final Map<String, Map<Integer, AtomicInteger>> packageCounters = new ConcurrentHashMap<String, Map<Integer, AtomicInteger>>();
private final Map<String, Long> leavingPackages = new ConcurrentHashMap<String, Long>();
private final Map<String, Long> leavingPackages = new ConcurrentHashMap<String, Long>();
private final Set<String> checkLeavingNext = new TreeSet<String>();
private final ScheduledThreadPoolExecutor singleExecutor = new ScheduledThreadPoolExecutor(0x2);
protected Context mContext;
Expand All @@ -46,84 +45,23 @@ abstract class ActivityReceiver extends BroadcastReceiver {
protected String vpnEstablishedActivity = null;
private boolean screen = false;
private ScheduledFuture<?> leavingFuture;
protected Map<String, List<String>> mActivityRecord=new ConcurrentHashMap<String, List<String>>();


public ActivityReceiver(Context context, Map<String, Boolean> preventPackages) {
mContext = context;
mPreventPackages = preventPackages;
}

protected int countCounter(String packageName) {
return countCounter(-1, packageName);
}

private int countCounter(int currentPid, String packageName) {
int count = 0;
Map<Integer, AtomicInteger> values = packageCounters.get(packageName);
if (values == null) {
return count;
}
Iterator<Map.Entry<Integer, AtomicInteger>> iterator = values.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Integer, AtomicInteger> entry = iterator.next();
int pid = entry.getKey();
if (pid == currentPid || checkPid(pid, packageName)) {
count += entry.getValue().get();
} else {
LogUtils.logIgnore(entry.getKey(), packageName);
iterator.remove();
}
if(mActivityRecord.containsKey(packageName)){
List activities = mActivityRecord.get(packageName);
return activities.size();
}
return count;
return -1;
}

private boolean checkPid(int pid, String packageName) {
Integer uid = packageUids.get(packageName);
if (uid == null) {
return false;
}
try {
if (HideApiUtils.getUidForPid(pid) != uid) {
return false;
}
} catch (Throwable t) { // NOSONAR
PreventLog.e("cannot get uid for " + pid, t);
}
String processName = getProcessName(uid, pid, packageName);
if (isNormalProcessName(processName, packageName)) {
return true;
}
PreventLog.v("pid: " + pid + ", package: " + packageName + ", process: " + processName);
Set<String> abnormalPackages = abnormalProcesses.get(processName);
return abnormalPackages != null && abnormalPackages.contains(packageName);
}

private String getProcessName(int uid, int pid, String packageName) {
String[] packages = mContext.getPackageManager().getPackagesForUid(uid);
if (packages != null && packages.length == 1) {
return packageName;
} else {
return SystemHook.getProcessName(pid);
}
}

private boolean isNormalProcessName(String processName, String packageName) {
return (processName != null) && (processName.equals(packageName)
|| processName.startsWith(packageName + ":")
|| "<pre-initialized>".equals(processName));
}

private void setAbnormalProcessIfNeeded(String processName, String packageName) {
if (!isNormalProcessName(processName, packageName)) {
Set<String> abnormalProcess = abnormalProcesses.get(processName);
if (abnormalProcess == null) {
abnormalProcess = new HashSet<String>();
abnormalProcesses.put(processName, abnormalProcess);
}
if (abnormalProcess.add(packageName)) {
PreventLog.d("package " + packageName + " has abnormal process: " + processName);
}
}
}

public void onLaunchActivity(Object activityRecord) {
String packageName = ActivityRecordUtils.getPackageName(activityRecord);
Expand All @@ -135,29 +73,27 @@ public void onLaunchActivity(Object activityRecord) {
if (mPreventPackages.containsKey(packageName)) {
mPreventPackages.put(packageName, false);
}
int pid = ActivityRecordUtils.getPid(activityRecord);
int uid = ActivityRecordUtils.getUid(activityRecord);
String processName = ActivityRecordUtils.getInfo(activityRecord).processName;
setAbnormalProcessIfNeeded(processName, packageName);
if (uid > 0) {
packageUids.put(packageName, uid);
}
Map<Integer, AtomicInteger> packageCounter = packageCounters.get(packageName);
if (packageCounter == null) {
packageCounter = new LinkedHashMap<Integer, AtomicInteger>();
packageCounters.put(packageName, packageCounter);
}
AtomicInteger pidCounter = packageCounter.get(pid);
if (pidCounter == null) {
pidCounter = new AtomicInteger();
packageCounter.put(pid, pidCounter);
}
pidCounter.incrementAndGet();
int count = countCounter(pid, packageName);
if (count == 1) {
SystemHook.checkSync(packageName);

if(mActivityRecord.containsKey(packageName)){
String activityName = ActivityRecordUtils.getActivityName(activityRecord);
if(activityName!=null){
List activities = mActivityRecord.get(packageName);
if(activities.contains(activityName)){

}else{
activities.add(activityName);
}
LogUtils.logActivity(activityName+"activity already started", packageName, activities.size());
}
}else{
String activityName = ActivityRecordUtils.getActivityName(activityRecord);
if(activityName!=null){
List activities = new ArrayList();
activities.add(activityName);
mActivityRecord.put(packageName,activities);
LogUtils.logActivity("start activity", packageName, 1);
}
}
LogUtils.logActivity("start activity", packageName, count);
removeLeaving(packageName);
}

Expand All @@ -167,25 +103,25 @@ public boolean onDestroyActivity(Object activityRecord) {
PreventLog.e("package " + packageName + " destroyed ");
return false;
}
Map<Integer, AtomicInteger> packageCounter = packageCounters.get(packageName);
if (packageCounter != null) {
int pid = ActivityRecordUtils.getPid(activityRecord);
AtomicInteger pidCounter = packageCounter.get(pid);
if (pidCounter != null) {
pidCounter.decrementAndGet();

if(mActivityRecord.containsKey(packageName)){
String activityName = ActivityRecordUtils.getActivityName(activityRecord);
if(activityName!=null){
List activities = mActivityRecord.get(packageName);
if(activities.contains(activityName)){
activities.remove(activityName);
LogUtils.logActivity("destroy activity", packageName, activities.size());
}
if (activities.size() > 0) {
return false;
}
}
}
int count = countCounter(packageName);
LogUtils.logActivity("destroy activity", packageName, count);
if (count > 0) {
return false;
}
SystemHook.updateRunningGapps(packageName, false);
if (mPreventPackages.containsKey(packageName)) {
mPreventPackages.put(packageName, true);
LogUtils.logForceStop("destroy activity", packageName, "if needed in " + SystemHook.TIME_DESTROY + "s");
SystemHook.checkForceStop(packageName, SystemHook.TIME_DESTROY);

//SystemHook.checkRunningServices(packageName, SystemHook.TIME_DESTROY);
} else {
SystemHook.checkRunningServices(null, SystemHook.TIME_DESTROY);
Expand Down Expand Up @@ -410,18 +346,15 @@ public void onAppDied(Object processRecord) {
}

private boolean shouldStop(String packageName, int pid) {
countCounter(packageName);
Map<Integer, AtomicInteger> values = packageCounters.get(packageName);
if (values == null) {
return true;
if(mActivityRecord.containsKey(packageName)){
List activities = mActivityRecord.get(packageName);
return activities.isEmpty();
}
Set<Integer> pids = new HashSet<Integer>(values.keySet());
pids.remove(pid);
return pids.isEmpty();
return true;
}

protected void removePackageCounters(String packageName) {
packageCounters.remove(packageName);
mActivityRecord.remove(packageName);
}

protected void onActivityRequestAudioFocus(int uid, int pid, String clientId, String packageName) {
Expand All @@ -440,11 +373,10 @@ protected void onActivityAbandonAudioFocus(int uid, int pid, String clientId) {
for (String key : keys) {
audioFocusedActivity.remove(key);
}
/* int count = countCounter(packageName);
int count = countCounter(packageName);
if (count == 0) {
SystemHook.checkForceStop(packageName, SystemHook.TIME_DESTROY);
}
*/
}
}

Expand All @@ -460,12 +392,10 @@ protected void onActivityLostAudioFocusOnDeath(String clientId) {
for (String key : keys) {
audioFocusedActivity.remove(key);
}
/*
int count = countCounter(packageName);
if (count == 0) {
SystemHook.checkForceStop(packageName, SystemHook.TIME_DESTROY);
}
*/
}
}

Expand All @@ -475,12 +405,10 @@ protected void onActivityEstablishVpnConnection(String packageName) {

protected void onVpnConnectionDisconnected() {
if (vpnEstablishedActivity!=null) {
/*
int count = countCounter(vpnEstablishedActivity);
if (count == 0) {
SystemHook.checkForceStop(vpnEstablishedActivity, SystemHook.TIME_DESTROY);
}
*/
vpnEstablishedActivity = null;
}
}
Expand Down
34 changes: 19 additions & 15 deletions app/src/me/piebridge/prevent/framework/SystemHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ public final class SystemHook {
private static final Map<String, Boolean> syncPackages = new HashMap<String, Boolean>();
private static final HashMap<String, String> audioFocusedActivity = new HashMap<String, String>();
private static String vpnEstablishedActivity=null;
// private static final List<String> onCleanUpRemovedTaskPandingVpnEstablishedActivity=new ArrayList<String>();
// private static final List<String> onDestroyActivityTaskPandingVpnEstablishedActivity=new ArrayList<String>();
// private static final HashMap<String, String> onCleanUpRemovedTaskPandingAudioFocusedActivity=new HashMap<String, String>();
// private static final List<String> onCleanUpRemovedTaskPandingVpnEstablishedActivity=new ArrayList<String>();
// private static final List<String> onDestroyActivityTaskPandingVpnEstablishedActivity=new ArrayList<String>();
// private static final HashMap<String, String> onCleanUpRemovedTaskPandingAudioFocusedActivity=new HashMap<String, String>();
//private static final HashMap<String, Object> onDestroyActivityPandingAudioFocusedActivity=new HashMap<String, Object>();
private static final Object lock = new Object();
private static Context mContext;
Expand Down Expand Up @@ -470,9 +470,9 @@ private static void doKillNoFather() {
String[] names = mContext.getPackageManager().getPackagesForUid(uid);
String name=null;
if (names != null) {
name= Arrays.asList(names).toString();
name= Arrays.asList(names).toString();
}
//PreventLog.d("doKillNoFather uid:"+uid+" pid:"+pid+" ppid:"+HideApiUtils.getParentPid(pid)+" name:"+name);
//PreventLog.d("doKillNoFather uid锛?+uid+" pid:"+pid+" ppid:"+HideApiUtils.getParentPid(pid)+" name:"+name);
if (HideApiUtils.getParentPid(pid) == 1 && uid >= PackageUtils.FIRST_APPLICATION_UID) {
killIfNeed(uid, pid);
}
Expand All @@ -481,11 +481,11 @@ private static void doKillNoFather() {
}

private static void killIfNeed(int uid, int pid) {
PreventLog.d("killIfNeed uid"+uid+" pid:"+pid);
PreventLog.d("killIfNeed uid:"+uid+" pid:"+pid);
String[] names = mContext.getPackageManager().getPackagesForUid(uid);
if (names == null || isPrevent(names)) {
for(String name:names){
PreventLog.d("killIfNeed name"+name);
PreventLog.d("killIfNeed name:"+name);
}
Process.killProcess(pid);
String name;
Expand Down Expand Up @@ -520,6 +520,7 @@ public static void restorePrevent(String packageName) {
public static void onLaunchActivity(Object activityRecord) {
expired = true;
currentPackageName = ActivityRecordUtils.getPackageName(activityRecord);

PreventLog.v("launch, current: " + currentPackageName);
if (systemReceiver != null) {
systemReceiver.onLaunchActivity(activityRecord);
Expand Down Expand Up @@ -780,8 +781,11 @@ public static boolean forceStopPackage(String packageName, boolean force) {
inactive(packageName);
setBackground(packageName, AppOpsManager.MODE_IGNORED);
return false;
} else if (systemReceiver.countCounter(packageName)>0) {
PreventLog.i(packageName + "has activities, seems not safe to force stop");
return false;
} else if (getCurrentPackageNames().contains(packageName)) {
PreventLog.i(packageName + " seems not safe to force stop");
PreventLog.i(packageName + "is current package, seems not safe to force stop");
return false;
} else if (PackageUtils.isInputMethod(mContext, packageName)) {
PreventLog.i(packageName + " inputmethod cannot be force stoped");
Expand All @@ -794,13 +798,13 @@ public static boolean forceStopPackage(String packageName, boolean force) {
}

public static void setBackground(String packageName, int mode) {
try {
IAppOpsService appOpsService = IAppOpsService.Stub.asInterface(ServiceManager.getService(Context.APP_OPS_SERVICE));
int packageUid = AppGlobals.getPackageManager().getPackageUid(packageName, PackageManager.MATCH_UNINSTALLED_PACKAGES, 0);
appOpsService.setMode(AppOpsManager.OP_RUN_IN_BACKGROUND, packageUid, packageName, mode);
} catch (RemoteException e) {
PreventLog.d("cannot set background for " + packageName, e);
}
try {
IAppOpsService appOpsService = IAppOpsService.Stub.asInterface(ServiceManager.getService(Context.APP_OPS_SERVICE));
int packageUid = AppGlobals.getPackageManager().getPackageUid(packageName, PackageManager.MATCH_UNINSTALLED_PACKAGES, 0);
appOpsService.setMode(AppOpsManager.OP_RUN_IN_BACKGROUND, packageUid, packageName, mode);
} catch (RemoteException e) {
PreventLog.d("cannot set background for " + packageName, e);
}
}

public static Context getContext() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,13 @@ private static void addPackage(Collection<String> packageNames, Object stack, St
String packageName = ActivityRecordUtils.getPackageName(mActivity);
PreventLog.d("activityRecord: " + mActivity + ", packageName: " + packageName + ", message: " + message);
if (packageName != null) {
packageNames.add(packageName);
if(checkVisible){
if(!isVisible(stack,mActivity,message)){
packageNames.add(packageName);
}
}else{
packageNames.add(packageName);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,7 @@ public static int getUid(Object target) {
}
}

public static String getActivityName(Object target) {
return (String) getField(target, "stringName");
}
}
6 changes: 0 additions & 6 deletions app/src/me/piebridge/prevent/ui/AdvancedSettingsActivity.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
package me.piebridge.prevent.ui;

import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.view.MenuItem;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import me.piebridge.prevent.R;
import me.piebridge.prevent.common.PreventIntent;
import me.piebridge.prevent.ui.util.DeprecatedUtils;
import me.piebridge.prevent.ui.util.PreventUtils;

/**
* Created by thom on 15/10/3.
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
include ':app',":groupedadapter"
rootProject.name = "blackzone-pro"
rootProject.name = "blackzone-new"

0 comments on commit 64635f0

Please sign in to comment.