Handle devices without Wi-Fi

This commit is contained in:
M66B 2015-12-08 17:00:40 +01:00
parent adecd479dc
commit c8c36f1c24
6 changed files with 84 additions and 37 deletions

View File

@ -9,7 +9,7 @@ model {
applicationId = "eu.faircode.netguard"
minSdkVersion.apiLevel = 21
targetSdkVersion.apiLevel = 23
versionCode = 2015120801
versionCode = 2015120802
versionName = "0.54"
archivesBaseName = "NetGuard-v$versionName-$versionCode"
}

View File

@ -162,6 +162,17 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere
pref_technical_subscription.setOnPreferenceClickListener(listener);
updateTechnicalInfo();
// Handle devices without wifi
if (!Util.hasWifi(this)) {
PreferenceCategory defaults = (PreferenceCategory) screen.findPreference("category_defaults");
defaults.removePreference(screen.findPreference("whitelist_wifi"));
defaults.removePreference(screen.findPreference("screen_wifi"));
PreferenceCategory options = (PreferenceCategory) screen.findPreference("category_options");
options.removePreference(screen.findPreference("wifi_home"));
options.removePreference(screen.findPreference("use_metered"));
}
// Handle devices without telephony
if (!Util.hasTelephony(this)) {
PreferenceCategory defaults = (PreferenceCategory) screen.findPreference("category_defaults");

View File

@ -99,7 +99,8 @@ public class Rule implements Comparable<Rule> {
SharedPreferences roaming = context.getSharedPreferences("roaming", Context.MODE_PRIVATE);
// Get settings
boolean telephony = Util.hasTelephony(context);
boolean haswifi = Util.hasWifi(context);
boolean hastelephony = Util.hasTelephony(context);
boolean default_wifi = prefs.getBoolean("whitelist_wifi", true);
boolean default_other = prefs.getBoolean("whitelist_other", true);
boolean default_screen_wifi = prefs.getBoolean("screen_wifi", true);
@ -160,7 +161,12 @@ public class Rule implements Comparable<Rule> {
rule.screen_other = screen_other.getBoolean(info.packageName, rule.screen_other_default);
rule.roaming = roaming.getBoolean(info.packageName, rule.roaming_default);
if (!telephony) {
if (!haswifi) {
rule.wifi_blocked = true;
rule.screen_wifi = false;
}
if (!hastelephony) {
rule.other_blocked = true;
rule.screen_other = false;
}
@ -168,7 +174,7 @@ public class Rule implements Comparable<Rule> {
if (pre_related.containsKey(info.packageName))
rule.related = pre_related.get(info.packageName);
rule.updateChanged(default_wifi, default_other, default_roaming, telephony);
rule.updateChanged(default_wifi, default_other, default_roaming, haswifi, hastelephony);
listRules.add(rule);
}
@ -180,10 +186,10 @@ public class Rule implements Comparable<Rule> {
return listRules;
}
private void updateChanged(boolean default_wifi, boolean default_other, boolean default_roaming, boolean telephony) {
changed = (wifi_blocked != default_wifi ||
private void updateChanged(boolean default_wifi, boolean default_other, boolean default_roaming, boolean wifi, boolean telephony) {
changed = (wifi && wifi_blocked != default_wifi ||
(telephony && other_blocked != default_other) ||
(wifi_blocked && screen_wifi != screen_wifi_default) ||
(wifi && wifi_blocked && screen_wifi != screen_wifi_default) ||
(telephony && other_blocked && screen_other != screen_other_default) ||
(telephony && (!other_blocked || screen_other) && roaming != default_roaming));
}
@ -193,8 +199,9 @@ public class Rule implements Comparable<Rule> {
boolean default_wifi = prefs.getBoolean("whitelist_wifi", true);
boolean default_other = prefs.getBoolean("whitelist_other", true);
boolean default_roaming = prefs.getBoolean("whitelist_roaming", true);
boolean wifi = Util.hasWifi(context);
boolean telephony = Util.hasTelephony(context);
updateChanged(default_wifi, default_other, default_roaming, telephony);
updateChanged(default_wifi, default_other, default_roaming, wifi, telephony);
}
@Override

View File

@ -57,6 +57,7 @@ public class RuleAdapter extends RecyclerView.Adapter<RuleAdapter.ViewHolder> im
private static final String TAG = "NetGuard.Adapter";
private Activity context;
private boolean wifi;
private boolean telephony;
private boolean debuggable;
private int colorText;
@ -75,11 +76,12 @@ public class RuleAdapter extends RecyclerView.Adapter<RuleAdapter.ViewHolder> im
public ImageView ivExpander;
public TextView tvName;
public LinearLayout llWifi;
public CheckBox cbWifi;
public ImageView ivScreenWifi;
public LinearLayout llOther;
public CheckBox cbOther;
public ImageView ivScreenWifi;
public ImageView ivScreenOther;
public TextView tvRoaming;
@ -89,10 +91,14 @@ public class RuleAdapter extends RecyclerView.Adapter<RuleAdapter.ViewHolder> im
public TextView tvVersion;
public TextView tvDisabled;
public TextView tvInternet;
public LinearLayout llWifiAttr;
public CheckBox cbScreenWifi;
public LinearLayout llOtherAttr;
public CheckBox cbScreenOther;
public CheckBox cbRoaming;
public ImageButton btnSettings;
public Button btnLaunch;
@ -105,11 +111,12 @@ public class RuleAdapter extends RecyclerView.Adapter<RuleAdapter.ViewHolder> im
ivExpander = (ImageView) itemView.findViewById(R.id.ivExpander);
tvName = (TextView) itemView.findViewById(R.id.tvName);
llWifi = (LinearLayout) itemView.findViewById(R.id.llWifi);
cbWifi = (CheckBox) itemView.findViewById(R.id.cbWifi);
ivScreenWifi = (ImageView) itemView.findViewById(R.id.ivScreenWifi);
llOther = (LinearLayout) itemView.findViewById(R.id.llOther);
cbOther = (CheckBox) itemView.findViewById(R.id.cbOther);
ivScreenWifi = (ImageView) itemView.findViewById(R.id.ivScreenWifi);
ivScreenOther = (ImageView) itemView.findViewById(R.id.ivScreenOther);
tvRoaming = (TextView) itemView.findViewById(R.id.tvRoaming);
@ -119,10 +126,14 @@ public class RuleAdapter extends RecyclerView.Adapter<RuleAdapter.ViewHolder> im
tvVersion = (TextView) itemView.findViewById(R.id.tvVersion);
tvDisabled = (TextView) itemView.findViewById(R.id.tvDisabled);
tvInternet = (TextView) itemView.findViewById(R.id.tvInternet);
llWifiAttr = (LinearLayout) itemView.findViewById(R.id.llWifiAttr);
cbScreenWifi = (CheckBox) itemView.findViewById(R.id.cbScreenWifi);
llOtherAttr = (LinearLayout) itemView.findViewById(R.id.llOtherAttr);
cbScreenOther = (CheckBox) itemView.findViewById(R.id.cbScreenOther);
cbRoaming = (CheckBox) itemView.findViewById(R.id.cbRoaming);
btnSettings = (ImageButton) itemView.findViewById(R.id.btnSettings);
btnLaunch = (Button) itemView.findViewById(R.id.btnLaunch);
@ -156,6 +167,7 @@ public class RuleAdapter extends RecyclerView.Adapter<RuleAdapter.ViewHolder> im
public RuleAdapter(Activity context) {
this.context = context;
this.wifi = Util.hasWifi(context);
this.telephony = Util.hasTelephony(context);
this.debuggable = Util.isDebuggable(context);
@ -259,6 +271,8 @@ public class RuleAdapter extends RecyclerView.Adapter<RuleAdapter.ViewHolder> im
color = Color.argb(128, Color.red(color), Color.green(color), Color.blue(color));
holder.tvName.setTextColor(color);
holder.llWifi.setVisibility(wifi ? View.VISIBLE : View.GONE);
holder.cbWifi.setAlpha(wifiActive ? 1 : 0.5f);
holder.cbWifi.setOnCheckedChangeListener(null);
holder.cbWifi.setChecked(rule.wifi_blocked);
@ -296,6 +310,8 @@ public class RuleAdapter extends RecyclerView.Adapter<RuleAdapter.ViewHolder> im
holder.cbScreenWifi.setChecked(rule.screen_wifi);
holder.cbScreenWifi.setEnabled(rule.wifi_blocked);
holder.llWifiAttr.setVisibility(wifi ? View.VISIBLE : View.GONE);
holder.cbScreenWifi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

View File

@ -87,6 +87,11 @@ public class Util {
return pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
}
public static boolean hasWifi(Context context) {
PackageManager pm = context.getPackageManager();
return pm.hasSystemFeature(PackageManager.FEATURE_WIFI);
}
public static boolean isConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();

View File

@ -43,36 +43,43 @@
android:textAppearance="@android:style/TextAppearance.Material.Medium" />
</LinearLayout>
<RelativeLayout
android:layout_width="24dp"
android:layout_height="?android:attr/listPreferredItemHeightSmall">
<CheckBox
android:id="@+id/cbWifi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:button="@drawable/wifi" />
</RelativeLayout>
<LinearLayout
android:id="@+id/llWifi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="vertical">
android:orientation="horizontal">
<ImageView
android:id="@+id/ivScreenWifi"
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/screen_on" />
<RelativeLayout
android:layout_width="24dp"
android:layout_height="?android:attr/listPreferredItemHeightSmall">
<View
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_gravity="center_horizontal"
android:visibility="invisible" />
<CheckBox
android:id="@+id/cbWifi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:button="@drawable/wifi" />
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="vertical">
<ImageView
android:id="@+id/ivScreenWifi"
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/screen_on" />
<View
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_gravity="center_horizontal"
android:visibility="invisible" />
</LinearLayout>
</LinearLayout>
<LinearLayout
@ -174,6 +181,7 @@
android:visibility="gone" />
<LinearLayout
android:id="@+id/llWifiAttr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"