Skip to content

Commit

Permalink
[AppDetails] Reduce the amount of internal refreshes
Browse files Browse the repository at this point in the history
Reload on the relevant sections instead of refreshing the entire UI.

Signed-off-by: Muntashir Al-Islam <[email protected]>
  • Loading branch information
MuntashirAkon committed Nov 9, 2024
1 parent 56f3e95 commit 4a50598
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.github.muntashirakon.AppManager.R;
import io.github.muntashirakon.AppManager.ipc.ProxyBinder;
import io.github.muntashirakon.AppManager.logs.Log;
import io.github.muntashirakon.AppManager.utils.ExUtils;

public final class NetworkPolicyManagerCompat {
public static final String TAG = NetworkPolicyManagerCompat.class.getSimpleName();
Expand Down Expand Up @@ -88,14 +89,22 @@ public final class NetworkPolicyManagerCompat {

@NetPolicy
@RequiresPermission(ManifestCompat.permission.MANAGE_NETWORK_POLICY)
public static int getUidPolicy(int uid) throws RemoteException {
return getNetPolicyManager().getUidPolicy(uid);
public static int getUidPolicy(int uid) {
try {
return getNetPolicyManager().getUidPolicy(uid);
} catch (RemoteException e) {
return ExUtils.rethrowFromSystemServer(e);
}
}

@RequiresPermission(ManifestCompat.permission.MANAGE_NETWORK_POLICY)
public static void setUidPolicy(int uid, int policies) throws RemoteException {
public static void setUidPolicy(int uid, int policies) {
if (UserHandleHidden.isApp(uid)) {
getNetPolicyManager().setUidPolicy(uid, policies);
try {
getNetPolicyManager().setUidPolicy(uid, policies);
} catch (RemoteException e) {
ExUtils.rethrowFromSystemServer(e);
}
} else {
Log.w(TAG, "Cannot set policy %d to uid %d", policies, uid);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ public static boolean deleteApplicationCacheFilesAsUser(String packageName, int
}

@RequiresPermission(ManifestCompat.permission.FORCE_STOP_PACKAGES)
public static void forceStopPackage(String packageName, int userId) throws RemoteException, SecurityException {
public static void forceStopPackage(String packageName, int userId) throws SecurityException {
try {
ActivityManagerCompat.getActivityManager().forceStopPackage(packageName, userId);
BroadcastUtils.sendPackageAltered(ContextUtils.getContext(), new String[]{packageName});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -114,6 +115,8 @@ public class AppDetailsViewModel extends AndroidViewModel {
private final ExecutorService mExecutor = Executors.newFixedThreadPool(4);
private final CountDownLatch mPackageInfoWatcher = new CountDownLatch(1);
private final MutableLiveData<PackageInfo> mPackageInfoLiveData = new MutableLiveData<>();
private final MutableLiveData<Boolean> mTagsAlteredLiveData = new MutableLiveData<>();
private final MutableLiveData<AppDetailsComponentItem> mComponentChangedLiveData = new MutableLiveData<>();

@Nullable
private PackageInfo mPackageInfo;
Expand Down Expand Up @@ -177,6 +180,10 @@ public void onCleared() {
mExecutor.shutdownNow();
}

public MutableLiveData<Boolean> getTagsAlteredLiveData() {
return mTagsAlteredLiveData;
}

@UiThread
@NonNull
public LiveData<PackageInfo> setPackage(@NonNull ApkSource apkSource) {
Expand Down Expand Up @@ -507,6 +514,7 @@ public void updateRulesForComponent(@NonNull AppDetailsComponentItem componentIt
@ComponentRule.ComponentStatus String componentStatus) {
if (mExternalApk) return;
mExecutor.submit(() -> {
Optional.ofNullable(mReceiver).ifPresent(PackageIntentReceiver::pauseWatcher);
String componentName = componentItem.name;
synchronized (mBlockerLocker) {
waitForBlockerOrExit();
Expand All @@ -527,9 +535,7 @@ public void updateRulesForComponent(@NonNull AppDetailsComponentItem componentIt
// Commit changes
mBlocker.commit();
mBlocker.setReadOnly();
// FIXME: 18/1/22 Do not reload all components, only reload this component
// Update UI
reloadComponents();
Optional.ofNullable(mReceiver).ifPresent(PackageIntentReceiver::resumeWatcher);
}
});
}
Expand Down Expand Up @@ -708,7 +714,6 @@ public boolean setAppOpMode(AppDetailsAppOpItem appOpItem) {
if (mExternalApk) return false;
PackageInfo packageInfo = getPackageInfoInternal();
if (packageInfo == null) return false;
boolean isSuccessful = false;
try {
if (appOpItem.isAllowed()) {
appOpItem.disallowAppOp(packageInfo, mAppOpsManager);
Expand Down Expand Up @@ -899,6 +904,7 @@ private MutableLiveData<List<AppDetailsItem<?>>> observeInternal(@NonNull Mutabl
@AnyThread
public void load(@AppDetailsFragment.Property int property) {
mExecutor.submit(() -> {
Optional.ofNullable(mReceiver).ifPresent(PackageIntentReceiver::pauseWatcher);
switch (property) {
case AppDetailsFragment.ACTIVITIES:
loadActivities();
Expand Down Expand Up @@ -937,6 +943,7 @@ public void load(@AppDetailsFragment.Property int property) {
loadAppInfo();
break;
}
Optional.ofNullable(mReceiver).ifPresent(PackageIntentReceiver::resumeWatcher);
});
}

Expand Down Expand Up @@ -1019,10 +1026,14 @@ private void waitForBlockerOrExit() {

@WorkerThread
private void reloadComponents() {
mExecutor.submit(this::loadActivities);
mExecutor.submit(this::loadServices);
mExecutor.submit(this::loadReceivers);
mExecutor.submit(this::loadProviders);
mExecutor.submit(() -> {
Optional.ofNullable(mReceiver).ifPresent(PackageIntentReceiver::pauseWatcher);
loadActivities();
loadServices();
loadReceivers();
loadProviders();
Optional.ofNullable(mReceiver).ifPresent(PackageIntentReceiver::resumeWatcher);
});
}

@SuppressLint("WrongConstant")
Expand Down Expand Up @@ -1903,30 +1914,49 @@ private void loadSharedLibraries() {
*/
public static class PackageIntentReceiver extends PackageChangeReceiver {
final AppDetailsViewModel mModel;
volatile boolean mPauseWatcher = false;
int mChangeCount = 0;

public PackageIntentReceiver(@NonNull AppDetailsViewModel model) {
super(model.getApplication());
mModel = model;
}

public void resumeWatcher() {
if (mChangeCount > 0) {
mChangeCount = 0;
mModel.setPackageChanged();
}
mPauseWatcher = false;
}

public void pauseWatcher() {
mChangeCount = 0;
mPauseWatcher = true;
}

@Override
@WorkerThread
protected void onPackageChanged(Intent intent, @Nullable Integer uid, @Nullable String[] packages) {
boolean packageChanged = false;
if (uid != null) {
if (mModel.mPackageInfo != null && mModel.mPackageInfo.applicationInfo.uid == uid) {
Log.d(TAG, "Package is changed.");
mModel.setPackageChanged();
packageChanged = true;
}
} else if (packages != null) {
for (String packageName : packages) {
if (packageName.equals(mModel.mPackageName)) {
Log.d(TAG, "Package availability changed.");
mModel.setPackageChanged();
packageChanged = true;
break;
}
}
} else {
Log.d(TAG, "Locale changed.");
mModel.setPackageChanged();
}
if (packageChanged) {
if (mPauseWatcher) {
++mChangeCount;
} else mModel.setPackageChanged();
}
}
}
Expand Down
Loading

0 comments on commit 4a50598

Please sign in to comment.