Skip to content

Commit

Permalink
[Android] Replace start on boot option with Smart Connect
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasGaubert committed Dec 29, 2016
1 parent 860a73e commit 0815fe2
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 39 deletions.
8 changes: 7 additions & 1 deletion android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

<application
android:allowBackup="true"
Expand Down Expand Up @@ -39,7 +40,12 @@
</intent-filter>
</service>

<receiver android:name="com.texasgamer.zephyr.BootReceiver">
<receiver android:name="com.texasgamer.zephyr.receiver.WiFiReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver android:name="com.texasgamer.zephyr.receiver.BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
Expand Down
19 changes: 0 additions & 19 deletions android/app/src/main/java/com/texasgamer/zephyr/BootReceiver.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseApp;
import com.texasgamer.zephyr.Constants;
import com.texasgamer.zephyr.manager.ConfigManager;
import com.texasgamer.zephyr.manager.LoginManager;
import com.texasgamer.zephyr.manager.MetricsManager;
Expand Down Expand Up @@ -208,13 +207,13 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
}
});

findPreference(getString(R.string.pref_start_on_boot)).setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
findPreference(getString(R.string.pref_smart_connect)).setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Bundle b = new Bundle();
String newValueStr = newValue.toString().equals("true") ? "enabled" : "disabled";
b.putString(getString(R.string.analytics_param_new_value), newValueStr);
activity.mMetricsManager.logEvent(R.string.analytics_tap_start_on_boot, b);
activity.mMetricsManager.logEvent(R.string.analytics_tap_smart_connect, b);
return true;
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ public class ConfigManager {
private final String TAG = this.getClass().getSimpleName();
private final int CACHE_EXPIRATION_IN_SECONDS = 3600;

private Context mContext;
private FirebaseRemoteConfig mRemoteConfig;

public ConfigManager(Context context) {
mContext = context;

if (Constants.FIREBASE_REMOTE_CONFIG_ENABLED) {
FirebaseRemoteConfigSettings configSettings =
new FirebaseRemoteConfigSettings.Builder()
Expand All @@ -41,7 +38,7 @@ public ConfigManager(Context context) {
@Override
public void onSuccess(Void aVoid) {
boolean result = mRemoteConfig.activateFetched();
Log.i(TAG, "Remote config data fetched (" + result + ")");
Log.v(TAG, "Remote config data fetched (" + result + ")");
}
}
)
Expand Down Expand Up @@ -78,7 +75,7 @@ private boolean getBoolean(String key, boolean fallback) {
result = fallback;
}

Log.i(TAG, key + " --> " + result + " (" + fallback + ")");
Log.v(TAG, key + " --> " + result + " (" + fallback + ")");
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ public MetricsManager(Context context) {
}

public void logEvent(@StringRes int iri, Bundle extras) {
Log.i(TAG, mContext.getString(iri) + ": " + (extras != null ? extras.toString() : "null"));
Log.v(TAG, mContext.getString(iri) + ": " + (extras != null ? extras.toString() : "null"));
firebaseEvent(iri, extras);
fabricEvent(iri, extras);
}

public void logLogin(String method, boolean success) {
Log.i(TAG, "login: " + method + " " + success);
Log.v(TAG, "login: " + method + " " + success);

if (Constants.FIREBASE_ANALYTICS_ENABLED) {
Bundle b = new Bundle();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.texasgamer.zephyr.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.preference.PreferenceManager;

import com.texasgamer.zephyr.R;
import com.texasgamer.zephyr.service.NotificationService;
import com.texasgamer.zephyr.service.SocketService;

public class BootReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Intent socketService = new Intent(context, SocketService.class);
context.startService(socketService);

Intent notificationService = new Intent(context, NotificationService.class);
context.startService(notificationService);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.texasgamer.zephyr.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.preference.PreferenceManager;
import android.util.Log;

import com.texasgamer.zephyr.R;
import com.texasgamer.zephyr.service.NotificationService;

public class WiFiReceiver extends BroadcastReceiver {

private final String TAG = this.getClass().getSimpleName();

@Override
public void onReceive(Context context, Intent intent) {
if (!PreferenceManager.getDefaultSharedPreferences(context).getBoolean(context.getString(R.string.pref_smart_connect), true)) {
Log.i(TAG, "Smart connect is disabled, so ignoring network state change.");
}

if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){
NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
if (info.isConnected()) {
String address = PreferenceManager.getDefaultSharedPreferences(context).getString(context.getString(R.string.pref_last_addr), "");

if (address.trim().isEmpty()) {
Log.i(TAG, "No previously known address, not attempting to connect!");
}

Log.i(TAG, "Connected to WiFi, attempting to connect to the server...");
Intent i = new Intent("com.texasgamer.zephyr.SOCKET_SERVICE");
i.putExtra("type", "connect");
i.putExtra("address", address);
context.sendBroadcast(i);
} else {
Log.i(TAG, "No longer connected to WiFi, attempting to disconnect from the server...");
Intent i = new Intent("com.texasgamer.zephyr.SOCKET_SERVICE");
i.putExtra("type", "disconnect");
context.sendBroadcast(i);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import com.texasgamer.zephyr.manager.MetricsManager;
import com.texasgamer.zephyr.R;
import com.texasgamer.zephyr.util.NetworkUtils;

import org.json.JSONArray;
import org.json.JSONException;
Expand Down Expand Up @@ -80,6 +81,12 @@ private void connect(String address) {
return;
}

if(PreferenceManager.getDefaultSharedPreferences(this).getBoolean(getString(R.string.pref_smart_connect), true) && !NetworkUtils.isConnectedToWiFi(this)) {
Log.i(TAG, "Not connected to WiFi, not attempting to connect!");
} else if(!NetworkUtils.isConnectedToWiFi(this)) {
Log.i(TAG, "Not connected to WiFi, but trying to connect anyways...");
}

Log.i(TAG, "Connecting to " + address + "...");

serverAddr = address;
Expand All @@ -94,10 +101,8 @@ private void connect(String address) {
e.printStackTrace();
}


if(socket != null) {
setUpEvents();

socket.connect();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.texasgamer.zephyr.util;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

public class NetworkUtils {

public static boolean isConnectedToWiFi(Context context) {
WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

if (wifiMgr.isWifiEnabled()) { // WiFi adapter is on
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
if (wifiInfo.getNetworkId() == -1) {
return false; // Not connected to an access point
}
return true; // Connected to an access point
} else {
return false; // WiFi adapter is OFF
}
}
}
8 changes: 4 additions & 4 deletions android/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<!-- Preferences -->
<string name="pref_first_run">firstRun</string>
<string name="pref_last_addr">lastAddr</string>
<string name="pref_start_on_boot">startOnBoot</string>
<string name="pref_smart_connect">smartConnect</string>
<string name="pref_device_name">deviceName</string>
<string name="pref_app_notif_base">appNotif</string>
<string name="pref_version">version</string>
Expand All @@ -60,8 +60,8 @@
<!-- General Settings -->
<string name="pref_header_general">General</string>

<string name="pref_title_start_on_boot">Connect on Boot</string>
<string name="pref_description_start_on_boot">Automatically connect when your device boots up.</string>
<string name="pref_title_smart_connect">Smart Connect</string>
<string name="pref_description_smart_connect">Automatically attempt to connect when connected to WiFi.</string>

<string name="pref_title_device_name">Device Name</string>
<string name="pref_default_device_name">Android Device</string>
Expand Down Expand Up @@ -107,7 +107,7 @@
<string name="analytics_tap_profile_card_logout">analytics_tap_profile_card_logout</string>

<string name="analytics_tap_general_prefs">tap_general_prefs</string>
<string name="analytics_tap_start_on_boot">tap_start_on_boot</string>
<string name="analytics_tap_smart_connect">tap_smart_connect</string>
<string name="analytics_tap_device_name">tap_device_name</string>
<string name="analytics_event_empty_device_name">empty_device_name</string>
<string name="analytics_tap_notif_prefs">tap_notif_prefs</string>
Expand Down
6 changes: 3 additions & 3 deletions android/app/src/main/res/xml/pref_general.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

<SwitchPreference
android:defaultValue="true"
android:key="@string/pref_start_on_boot"
android:summary="@string/pref_description_start_on_boot"
android:title="@string/pref_title_start_on_boot" />
android:key="@string/pref_smart_connect"
android:summary="@string/pref_description_smart_connect"
android:title="@string/pref_title_smart_connect" />

<EditTextPreference
android:capitalize="words"
Expand Down

0 comments on commit 0815fe2

Please sign in to comment.