Bring back the settings button, cleanup

This commit is contained in:
M66B 2017-11-10 16:12:09 +01:00
parent 66cf9dd012
commit 477a624682
3 changed files with 75 additions and 182 deletions

View File

@ -69,6 +69,7 @@ import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
@ -120,13 +121,11 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> im
public TextView tvUid;
public TextView tvPackage;
public TextView tvVersion;
public TextView tvDescription;
public TextView tvInternet;
public TextView tvDisabled;
public Button btnRelated;
public ImageButton ibSettings;
public ImageButton ibDatasaver;
public ImageButton ibLaunch;
public CheckBox cbApply;
@ -181,13 +180,11 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> im
tvUid = itemView.findViewById(R.id.tvUid);
tvPackage = itemView.findViewById(R.id.tvPackage);
tvVersion = itemView.findViewById(R.id.tvVersion);
tvDescription = itemView.findViewById(R.id.tvDescription);
tvInternet = itemView.findViewById(R.id.tvInternet);
tvDisabled = itemView.findViewById(R.id.tvDisabled);
btnRelated = itemView.findViewById(R.id.btnRelated);
ibSettings = itemView.findViewById(R.id.ibSettings);
ibDatasaver = itemView.findViewById(R.id.ibDatasaver);
ibLaunch = itemView.findViewById(R.id.ibLaunch);
cbApply = itemView.findViewById(R.id.cbApply);
@ -437,8 +434,6 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> im
holder.tvUid.setText(Integer.toString(rule.uid));
holder.tvPackage.setText(rule.packageName);
holder.tvVersion.setText(rule.version);
holder.tvDescription.setVisibility(rule.description == null ? View.GONE : View.VISIBLE);
holder.tvDescription.setText(rule.description);
// Show application state
holder.tvInternet.setVisibility(rule.internet ? View.GONE : View.VISIBLE);
@ -457,49 +452,36 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> im
});
// Launch application settings
holder.ibSettings.setVisibility(rule.settings == null ? View.GONE : View.VISIBLE);
holder.ibSettings.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
context.startActivity(rule.settings);
}
});
if (rule.expanded) {
Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + rule.packageName));
final Intent settings = (intent.resolveActivity(context.getPackageManager()) == null ? null : intent);
// Data saver
holder.ibDatasaver.setVisibility(rule.datasaver == null ? View.GONE : View.VISIBLE);
holder.ibDatasaver.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
context.startActivity(rule.datasaver);
}
});
holder.ibSettings.setVisibility(settings == null ? View.GONE : View.VISIBLE);
holder.ibSettings.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
context.startActivity(settings);
}
});
} else
holder.ibSettings.setVisibility(View.GONE);
// Launch application
holder.ibLaunch.setVisibility(View.GONE);
holder.ibLaunch.setHasTransientState(true);
new AsyncTask<Rule, Object, Intent>() {
@Override
protected Intent doInBackground(Rule... rule) {
try {
return context.getPackageManager().getLaunchIntentForPackage(rule[0].packageName);
} catch (Throwable ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
return null;
}
}
if (rule.expanded) {
Intent intent = context.getPackageManager().getLaunchIntentForPackage(rule.packageName);
final Intent launch = (intent == null ||
intent.resolveActivity(context.getPackageManager()) == null ? null : intent);
@Override
protected void onPostExecute(final Intent intent) {
holder.ibLaunch.setHasTransientState(false);
holder.ibLaunch.setVisibility(intent == null ? View.GONE : View.VISIBLE);
holder.ibLaunch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
context.startActivity(intent);
}
});
}
}.execute(rule);
holder.ibLaunch.setVisibility(launch == null ? View.GONE : View.VISIBLE);
holder.ibLaunch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
context.startActivity(launch);
}
});
} else
holder.ibLaunch.setVisibility(View.GONE);
// Apply
holder.cbApply.setEnabled(rule.pkg);

View File

@ -20,18 +20,15 @@ package eu.faircode.netguard;
*/
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.XmlResourceParser;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Process;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.util.Log;
import org.xmlpull.v1.XmlPullParser;
@ -53,14 +50,10 @@ public class Rule {
public String packageName;
public int icon;
public String name;
public String description;
public String version;
public boolean system;
public boolean internet;
public boolean enabled;
public Intent launch;
public Intent settings;
public Intent datasaver;
public boolean pkg = true;
public boolean wifi_default = false;
@ -89,14 +82,9 @@ public class Rule {
private static List<PackageInfo> cachePackageInfo = null;
private static Map<PackageInfo, String> cacheLabel = new HashMap<>();
private static Map<PackageInfo, String> cacheDescription = new HashMap<>();
private static Map<String, Boolean> cacheSystem = new HashMap<>();
private static Map<String, Boolean> cacheInternet = new HashMap<>();
private static Map<PackageInfo, Boolean> cacheEnabled = new HashMap<>();
private static Map<String, Intent> cacheIntentLaunch = new HashMap<>();
private static Map<String, Intent> cacheIntentSettings = new HashMap<>();
private static Map<String, Intent> cacheIntentDatasaver = new HashMap<>();
private static Map<Integer, String[]> cachePackages = new HashMap<>();
private static List<PackageInfo> getPackages(Context context) {
if (cachePackageInfo == null) {
@ -114,15 +102,6 @@ public class Rule {
return cacheLabel.get(info);
}
private static String getDescription(PackageInfo info, Context context) {
if (!cacheDescription.containsKey(info)) {
PackageManager pm = context.getPackageManager();
CharSequence description = info.applicationInfo.loadDescription(pm);
cacheDescription.put(info, description == null ? null : description.toString());
}
return cacheDescription.get(info);
}
private static boolean isSystem(String packageName, Context context) {
if (!cacheSystem.containsKey(packageName))
cacheSystem.put(packageName, Util.isSystem(packageName, context));
@ -141,57 +120,14 @@ public class Rule {
return cacheEnabled.get(info);
}
private static Intent getIntentLaunch(String packageName, Context context) {
if (!cacheIntentLaunch.containsKey(packageName))
cacheIntentLaunch.put(packageName, context.getPackageManager().getLaunchIntentForPackage(packageName));
return cacheIntentLaunch.get(packageName);
}
private static Intent getIntentSettings(String packageName, Context context) {
if (!cacheIntentSettings.containsKey(packageName)) {
Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + packageName));
if (intent.resolveActivity(context.getPackageManager()) == null)
intent = null;
cacheIntentSettings.put(packageName, intent);
}
return cacheIntentSettings.get(packageName);
}
private static Intent getIntentDatasaver(String packageName, Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
if (!cacheIntentDatasaver.containsKey(packageName)) {
Intent intent = new Intent(
Settings.ACTION_IGNORE_BACKGROUND_DATA_RESTRICTIONS_SETTINGS,
Uri.parse("package:" + packageName));
if (intent.resolveActivity(context.getPackageManager()) == null)
intent = null;
cacheIntentDatasaver.put(packageName, intent);
}
return cacheIntentDatasaver.get(packageName);
} else
return null;
}
private static String[] getPackages(int uid, Context context) {
if (!cachePackages.containsKey(uid))
cachePackages.put(uid, context.getPackageManager().getPackagesForUid(uid));
return cachePackages.get(uid);
}
public static void clearCache(Context context) {
Log.i(TAG, "Clearing cache");
synchronized (context.getApplicationContext()) {
cachePackageInfo = null;
cacheLabel.clear();
cacheDescription.clear();
cacheSystem.clear();
cacheInternet.clear();
cacheEnabled.clear();
cacheIntentLaunch.clear();
cacheIntentSettings.clear();
cacheIntentDatasaver.clear();
cachePackages.clear();
}
DatabaseHelper dh = DatabaseHelper.getInstance(context);
@ -205,43 +141,27 @@ public class Rule {
this.version = info.versionName;
if (info.applicationInfo.uid == 0) {
this.name = context.getString(R.string.title_root);
this.description = null;
this.system = true;
this.internet = true;
this.enabled = true;
this.launch = null;
this.settings = null;
this.datasaver = null;
this.pkg = false;
} else if (info.applicationInfo.uid == 1013) {
this.name = context.getString(R.string.title_mediaserver);
this.description = null;
this.system = true;
this.internet = true;
this.enabled = true;
this.launch = null;
this.settings = null;
this.datasaver = null;
this.pkg = false;
} else if (info.applicationInfo.uid == 1021) {
this.name = context.getString(R.string.title_gpsdaemon);
this.description = null;
this.system = true;
this.internet = true;
this.enabled = true;
this.launch = null;
this.settings = null;
this.datasaver = null;
this.pkg = false;
} else if (info.applicationInfo.uid == 9999) {
this.name = context.getString(R.string.title_nobody);
this.description = null;
this.system = true;
this.internet = true;
this.enabled = true;
this.launch = null;
this.settings = null;
this.datasaver = null;
this.pkg = false;
} else {
Cursor cursor = null;
@ -249,23 +169,14 @@ public class Rule {
cursor = dh.getApp(this.packageName);
if (cursor.moveToNext()) {
this.name = cursor.getString(cursor.getColumnIndex("label"));
this.description = null;
this.system = cursor.getInt(cursor.getColumnIndex("system")) > 0;
this.internet = cursor.getInt(cursor.getColumnIndex("internet")) > 0;
this.enabled = cursor.getInt(cursor.getColumnIndex("enabled")) > 0;
} else {
this.name = getLabel(info, context);
this.description = null;
// this.description = getDescription(info, context);
this.system = isSystem(info.packageName, context);
this.internet = hasInternet(info.packageName, context);
this.enabled = isEnabled(info, context);
//this.launch = getIntentLaunch(info.packageName, context);
//this.settings = getIntentSettings(info.packageName, context);
//this.datasaver = getIntentDatasaver(info.packageName, context);
this.launch = null;
this.settings = null;
this.datasaver = null;
dh.addApp(this.packageName, this.name, this.system, this.internet, this.enabled);
}

View File

@ -1,12 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="8dp"
android:paddingEnd="4dp"
android:paddingRight="4dp"
android:paddingTop="8dp">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="8dp"
android:paddingEnd="4dp"
android:paddingRight="4dp"
android:paddingTop="8dp">
<LinearLayout
android:layout_width="match_parent"
@ -27,13 +27,13 @@
android:layout_height="32dp"
android:layout_gravity="center_vertical|center_horizontal"
android:alpha="0.5"
android:src="?attr/expander"/>
android:src="?attr/expander" />
<ImageView
android:id="@+id/ivIcon"
android:layout_width="?android:attr/listPreferredItemHeightSmall"
android:layout_height="?android:attr/listPreferredItemHeightSmall"
android:layout_gravity="center_vertical"/>
android:layout_gravity="center_vertical" />
<TextView
android:id="@+id/tvName"
@ -44,7 +44,7 @@
android:gravity="center_vertical"
android:paddingLeft="8dp"
android:paddingStart="8dp"
android:textAppearance="@style/TextMedium"/>
android:textAppearance="@style/TextMedium" />
</LinearLayout>
<TextView
@ -55,7 +55,7 @@
android:layout_marginLeft="4dp"
android:layout_marginStart="4dp"
android:textAppearance="@style/TextSmall"
android:textSize="12sp"/>
android:textSize="12sp" />
<RelativeLayout
android:id="@+id/rlLockdown"
@ -69,7 +69,7 @@
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_centerInParent="true"
android:src="@drawable/lockdown"/>
android:src="@drawable/lockdown" />
</RelativeLayout>
<LinearLayout
@ -88,7 +88,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:button="@drawable/wifi"/>
android:button="@drawable/wifi" />
</RelativeLayout>
<LinearLayout
@ -102,13 +102,13 @@
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/screen"/>
android:src="@drawable/screen" />
<View
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_gravity="center_horizontal"
android:visibility="invisible"/>
android:visibility="invisible" />
</LinearLayout>
</LinearLayout>
@ -128,7 +128,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:button="@drawable/other"/>
android:button="@drawable/other" />
</RelativeLayout>
<LinearLayout
@ -142,7 +142,7 @@
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/screen"/>
android:src="@drawable/screen" />
<TextView
android:id="@+id/tvRoaming"
@ -153,7 +153,7 @@
android:text="@string/title_roaming_symbol"
android:textAppearance="@style/TextSmall"
android:textColor="?attr/colorOff"
android:textSize="14sp"/>
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
@ -173,7 +173,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:textAppearance="@style/TextSmall"/>
android:textAppearance="@style/TextSmall" />
<TextView
android:id="@+id/tvPackage"
@ -181,14 +181,14 @@
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:textAppearance="@style/TextSmall"
android:textIsSelectable="true"/>
android:textIsSelectable="true" />
<TextView
android:id="@+id/tvVersion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:textAppearance="@style/TextSmall"/>
android:textAppearance="@style/TextSmall" />
<TextView
android:id="@+id/tvDescription"
@ -197,7 +197,7 @@
android:layout_marginTop="4dp"
android:textAppearance="@style/TextSmall"
android:textStyle="italic"
android:visibility="gone"/>
android:visibility="gone" />
<TextView
android:id="@+id/tvInternet"
@ -207,7 +207,7 @@
android:text="@string/title_internet"
android:textAppearance="@style/TextSmall"
android:textStyle="italic"
android:visibility="gone"/>
android:visibility="gone" />
<TextView
android:id="@+id/tvDisabled"
@ -217,7 +217,7 @@
android:text="@string/title_disabled"
android:textAppearance="@style/TextSmall"
android:textStyle="italic"
android:visibility="gone"/>
android:visibility="gone" />
<LinearLayout
android:layout_width="wrap_content"
@ -232,7 +232,7 @@
android:layout_gravity="center_vertical"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="@string/title_related"/>
android:text="@string/title_related" />
<ImageButton
android:id="@+id/ibDatasaver"
@ -242,7 +242,7 @@
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:src="?attr/iconDatasaver"
android:visibility="gone"/>
android:visibility="gone" />
<ImageButton
android:id="@+id/ibSettings"
@ -251,14 +251,14 @@
android:layout_gravity="center_vertical"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:src="?attr/iconSettings"/>
android:src="?attr/iconSettings" />
<ImageButton
android:id="@+id/ibLaunch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="?attr/iconLaunch"/>
android:src="?attr/iconLaunch" />
</LinearLayout>
<CheckBox
@ -266,14 +266,14 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="@string/title_apply"/>
android:text="@string/title_apply" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="@string/title_conditions"
android:textAppearance="@style/TextTitle"/>
android:textAppearance="@style/TextTitle" />
<LinearLayout
android:id="@+id/llScreenWifi"
@ -287,14 +287,14 @@
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_gravity="center_vertical"
android:src="@drawable/screen_on"/>
android:src="@drawable/screen_on" />
<CheckBox
android:id="@+id/cbScreenWifi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title_screen_wifi"
android:textAppearance="@style/TextSmall"/>
android:textAppearance="@style/TextSmall" />
</LinearLayout>
<LinearLayout
@ -309,14 +309,14 @@
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_gravity="center_vertical"
android:src="@drawable/screen_on"/>
android:src="@drawable/screen_on" />
<CheckBox
android:id="@+id/cbScreenOther"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title_screen_other"
android:textAppearance="@style/TextSmall"/>
android:textAppearance="@style/TextSmall" />
</LinearLayout>
<LinearLayout
@ -333,14 +333,14 @@
android:text="@string/title_roaming_symbol"
android:textAppearance="@style/TextSmall"
android:textColor="?attr/colorOff"
android:textSize="14sp"/>
android:textSize="14sp" />
<CheckBox
android:id="@+id/cbRoaming"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title_roaming"
android:textAppearance="@style/TextSmall"/>
android:textAppearance="@style/TextSmall" />
</LinearLayout>
<LinearLayout
@ -354,14 +354,14 @@
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_gravity="center_vertical"
android:src="@drawable/lockdown_off"/>
android:src="@drawable/lockdown_off" />
<CheckBox
android:id="@+id/cbLockdown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title_lockdown"
android:textAppearance="@style/TextSmall"/>
android:textAppearance="@style/TextSmall" />
</LinearLayout>
<ImageButton
@ -369,7 +369,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:src="?attr/iconDelete"/>
android:src="?attr/iconDelete" />
<LinearLayout
android:id="@+id/llFilter"
@ -389,7 +389,7 @@
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:text="@string/title_access"
android:textAppearance="@style/TextTitle"/>
android:textAppearance="@style/TextTitle" />
<ImageView
android:id="@+id/ivLive"
@ -398,7 +398,7 @@
android:layout_gravity="center_vertical"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:src="?attr/iconPause"/>
android:src="?attr/iconPause" />
</LinearLayout>
<LinearLayout
@ -413,7 +413,7 @@
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:textAppearance="@style/TextSmall"/>
android:textAppearance="@style/TextSmall" />
<Button
android:id="@+id/btnLogging"
@ -425,7 +425,7 @@
android:layout_marginRight="8dp"
android:minHeight="0dp"
android:minWidth="0dp"
android:text="@string/title_logging_configure"/>
android:text="@string/title_logging_configure" />
</LinearLayout>
<TextView
@ -434,13 +434,13 @@
android:layout_marginTop="4dp"
android:text="@string/title_precedence"
android:textAppearance="@style/TextSmall"
android:textStyle="italic"/>
android:textStyle="italic" />
<eu.faircode.netguard.ExpandedListView
android:id="@+id/lvAccess"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"/>
android:layout_marginTop="4dp" />
<ImageButton
android:id="@+id/btnClearAccess"
@ -448,14 +448,14 @@
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginTop="4dp"
android:src="?attr/iconDelete"/>
android:src="?attr/iconDelete" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="@string/title_options"
android:textAppearance="@style/TextTitle"/>
android:textAppearance="@style/TextTitle" />
<CheckBox
android:id="@+id/cbNotify"
@ -463,7 +463,7 @@
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="@string/title_notify"
android:textAppearance="@style/TextSmall"/>
android:textAppearance="@style/TextSmall" />
</LinearLayout>
</LinearLayout>
</LinearLayout>