Skip to content

Commit

Permalink
Move creation logic out of SelectedApplicationEntry.
Browse files Browse the repository at this point in the history
  • Loading branch information
msimonides-proton committed Sep 20, 2021
1 parent db165a8 commit c429db0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ public AppsViewHolder(View view) {
public void layoutAddRemove() {
item.setSelected(!item.isSelected());
if (item.isSelected()) {
addApp(item.getInfo().packageName);
addApp(item.getPackageName());
}
else {
removeApp(item.getInfo().packageName);
removeApp(item.getPackageName());
}
initSelection();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package com.protonvpn.android.ui.splittunneling

import android.Manifest
import android.app.ActivityManager
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.os.Process
import android.view.View
Expand Down Expand Up @@ -82,7 +83,7 @@ class AppsDialog : BaseDialog() {
val allApps = getInstalledInternetApps(requireContext().packageManager)
val sortedApps = withContext(Dispatchers.Default) {
allApps.forEach { app ->
if (selection.contains(app.info.packageName)) {
if (selection.contains(app.packageName)) {
app.isSelected = true
}
}
Expand All @@ -103,7 +104,7 @@ class AppsDialog : BaseDialog() {
private fun removeUninstalledApps(userData: UserData, allApps: List<SelectedApplicationEntry>) {
val userDataAppPackages = userData.splitTunnelApps
val allAppPackages = HashSet<String>(allApps.size)
allApps.mapTo(allAppPackages) { it.info.packageName }
allApps.mapTo(allAppPackages) { it.packageName }
userDataAppPackages
.filterNot { allAppPackages.contains(it) }
.forEach { userData.removeAppFromSplitTunnel(it) }
Expand All @@ -121,14 +122,23 @@ class AppsDialog : BaseDialog() {
(packageManager.checkPermission(Manifest.permission.INTERNET, appInfo.packageName)
== PackageManager.PERMISSION_GRANTED)
}.map { appInfo ->
SelectedApplicationEntry(packageManager, appInfo)
getAppMetadata(packageManager, appInfo)
}

ProtonLogger.log("getInstalledInternetApps: final code size: ${getCodeMemoryKb()} KB")
tryReleaseMemory(initialCodeSizeKb)
apps
}

private fun getAppMetadata(
packageManager: PackageManager,
appInfo: ApplicationInfo
): SelectedApplicationEntry {
val label = appInfo.loadLabel(packageManager)
val icon = appInfo.loadIcon(packageManager)
return SelectedApplicationEntry(appInfo.packageName, label.toString(), icon)
}

@Suppress("ExplicitGarbageCollectionCall")
private suspend fun tryReleaseMemory(initialCodeSizeKb: Int) {
// Loading application metadata with loadLabel and loadIcon increases memory use in the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
*/
package com.protonvpn.android.ui.splittunneling;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;

import java.text.Collator;
Expand All @@ -28,16 +26,15 @@

public class SelectedApplicationEntry implements Comparable<SelectedApplicationEntry> {

private final ApplicationInfo mInfo;
private final String mPackageName;
private final Drawable mIcon;
private final String mName;
private boolean mSelected;

public SelectedApplicationEntry(PackageManager packageManager, ApplicationInfo info) {
mInfo = info;
CharSequence name = info.loadLabel(packageManager);
mName = name == null ? info.packageName : name.toString();
mIcon = info.loadIcon(packageManager);
public SelectedApplicationEntry(@NonNull String packageName, @NonNull String label, @NonNull Drawable icon) {
mPackageName = packageName;
mName = label;
mIcon = icon;
}

public void setSelected(boolean selected) {
Expand All @@ -48,8 +45,9 @@ public boolean isSelected() {
return mSelected;
}

public ApplicationInfo getInfo() {
return mInfo;
@NonNull
public String getPackageName() {
return mPackageName;
}

public Drawable getIcon() {
Expand All @@ -65,4 +63,4 @@ public String toString() {
public int compareTo(@NonNull SelectedApplicationEntry another) {
return Collator.getInstance().compare(toString(), another.toString());
}
}
}

0 comments on commit c429db0

Please sign in to comment.