Skip to content

Commit

Permalink
Merge pull request #10 from gvillo/fix-issue-and-add-service-running-…
Browse files Browse the repository at this point in the history
…for-option

Fixes issue when app starts and adds service running for option
  • Loading branch information
borconi committed Oct 13, 2020
2 parents b0fb63a + 715855a commit 5d176c4
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 16 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "com.borconi.emil.wifilauncherforhur"
minSdkVersion 28
targetSdkVersion 30
versionCode 36
versionName "3.6"
versionCode 361
versionName "3.6.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
android:excludeFromRecents="true"
android:noHistory="true"/>

<service android:name=".services.WifiService"
android:foregroundServiceType="mediaProjection" />

<receiver
android:name=".receivers.BluetoothReceiver"
android:permission="android.permission.BLUETOOTH">
Expand All @@ -63,9 +66,6 @@
</intent-filter>
</receiver>

<service android:name=".services.WifiService"
android:foregroundServiceType="mediaProjection" />

<receiver
android:name=".receivers.WifiReceiver"
android:exported="false">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ protected void onCreate(Bundle savedInstanceState) {

if (getIntent().getBooleanExtra("DISPLAY_ACTION_PANEL", false)) {
findViewById(R.id.turn_on_button).setVisibility(View.INVISIBLE);
NotificationManager mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(WIFI_NOTIFICATION_ID);
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(WIFI_NOTIFICATION_ID);
startActivityForResult(new Intent(Settings.Panel.ACTION_WIFI), ACTION_WIFI_PANEL_REQUEST_CODE);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
}

@Override
@SuppressWarnings("unchecked")
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
LinearLayout v = (LinearLayout) super.onCreateView(inflater, container, savedInstanceState);

Expand Down Expand Up @@ -180,10 +181,18 @@ protected void setBluetoothDevicesSummary(MultiSelectListPreference bluetoothDev
return;
}
if (bluetoothDevices != null) {
if (bluetoothDevices.getValues() != null &&
bluetoothDevices.getEntries() != null && bluetoothDevices.getValues().size() > 0) {
bluetoothDevices.setSummary(bluetoothDevices.getValues().stream()
.map(v -> bluetoothDevices.getEntries()[bluetoothDevices.findIndexOfValue(v)]).collect(Collectors.joining(", ")));
Set<String> values = bluetoothDevices.getValues().stream()
.filter(v -> !v.equalsIgnoreCase("")).collect(Collectors.toSet());

if (values.size() > 0) {
bluetoothDevices.setSummary(values.stream()
.map(v -> {
int indexOfValue = bluetoothDevices.findIndexOfValue(v);
if (indexOfValue >= 0) {
return bluetoothDevices.getEntries()[indexOfValue];
}
return getString(R.string.settings_bluetooth_selected_bluetooth_devices_forgotten_device);
}).collect(Collectors.joining(", ")));
} else {
bluetoothDevices.setSummary(R.string.settings_bluetooth_selected_bluetooth_devices_description);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@

public class WifiService extends Service {
private static final int FIVE_SECONDS = 5000;
private static final int TWO_MINUTES = 60 * 1000 * 2;
private static final String STRING_EMPTY = "";
private static final String WIFI_STRING_FORMAT = "\"%s\"";
private static final int NOTIFICATION_ID = 1035;
Expand All @@ -68,6 +67,7 @@ public class WifiService extends Service {

public static boolean askingForLocation = false;

private int serviceTimeoutInMinutes = 60 * 1000 * 2; // Default 2 minutes.
private ConnectivityManager.NetworkCallback networkCallback;
private CarModeReceiver carModeReceiver;
private WifiLocalReceiver wifiLocalReceiver;
Expand Down Expand Up @@ -98,7 +98,8 @@ public void onCreate() {
tryToConnect();

// We will try to connect for 2 minutes, otherwise we will stop this service.
handler.postDelayed(StopServiceRunnable, TWO_MINUTES);
Log.d("WifiService", String.format("STOPPING SERVICE IN %s mins", serviceTimeoutInMinutes));
handler.postDelayed(StopServiceRunnable, serviceTimeoutInMinutes);
}

public void tryToConnect() {
Expand Down Expand Up @@ -304,6 +305,11 @@ protected Notification getNotification(Context context, String contentText) {
protected void setSharedPreferencesValues() {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

String serviceRunningFor = sharedPreferences.getString("service_running_for", getString(R.string.service_running_for_default_value));
if (serviceRunningFor != null) {
serviceTimeoutInMinutes = Integer.parseInt(serviceRunningFor) * 60 * 1000;
}

headunitWifiSsid = sharedPreferences.getString("headunitWifiSsid", getString(R.string.headunitWifiSsid_default_value));
headunitWifiWpa2Passphrase = sharedPreferences.getString("headunitWifiWpa2Passphrase", getString(R.string.headunitWifiWpa2Passphrase_default_value));
}
Expand Down Expand Up @@ -360,7 +366,7 @@ private void registerTryToConnectCallback() {
public void run() {
Log.d("WifiService", "STOP SERVICE RUNNABLE CHECK");
if (!isConnected) {
Log.d("WifiService", "STOPPING SERVICE BY TIME (2 min)");
Log.d("WifiService", String.format("STOPPING SERVICE BY TIME (%s min)", serviceTimeoutInMinutes));
stopForeground(true);
stopSelf();
} else {
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
<string name="settings_bluetooth_selected_bluetooth_devices_turn_on">Por favor encienda el bluetooth</string>
<string name="settings_bluetooth_selected_bluetooth_devices_bt_off">Bluetooth estaba apagado, re abrir para refrescar</string>
<string name="settings_bluetooth_selected_bluetooth_devices_no_devices">No hay dispositivos Bluetooth vinculados</string>
<string name="settings_bluetooth_selected_bluetooth_devices_forgotten_device">Dispositivo olvidado</string>
<string name="settings_advanced_options_enable_force_connect_button_title">Mostrar botón Forzar Conexión</string>
<string name="settings_advanced_options_enable_force_connect_button_description">Agrega un botón en la notificación del servicio para forzar la conexión a la pantalla del auto (necesita reiniciar el servicio)</string>
<string name="settings_advanced_options_start_service_manually_title">Iniciar servicio Wi-Fi manualmente</string>
<string name="settings_advanced_options_start_service_manually_description">Inicia el servicio que conecta Android Auto Inalámbrico.</string>
<string name="settings_advanced_options_service_running_for_title">Servicio funcionando por</string>
<string name="settings_wireless_headunit_ssid_title">Nombre de la Wi-Fi de la pantalla del auto</string>
<string name="settings_wireless_headunit_wpa2_passphrase_title">Contraseña de la Wi-Fi de la pantalla del auto</string>
<string name="settings_wireless_note">Nota: Considere que si la pantalla del auto está rooteada puede restablecer la configuracion del hotspot (HUR/AndroidAutoConnect) cada vez que inicia. La contraseña debe ser WPA2 Personal.</string>
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/res/values/array.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="service_running_for_entries">
<item>1 min</item>
<item>2 mins (default)</item>
<item>3 mins</item>
<item>4 mins</item>
<item>5 mins</item>
<item>10 mins</item>
</string-array>
<string-array name="service_running_for_entry_values">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>10</item>
</string-array>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/default_values.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="service_running_for_default_value" translatable="false">2</string>
<string name="headunitWifiSsid_default_value" translatable="false">HUR</string>
<string name="headunitWifiWpa2Passphrase_default_value" translatable="false">AndroidAutoConnect</string>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
<string name="settings_bluetooth_selected_bluetooth_devices_turn_on">Please turn on bluetooth</string>
<string name="settings_bluetooth_selected_bluetooth_devices_bt_off">Bluetooth was off, re open it again</string>
<string name="settings_bluetooth_selected_bluetooth_devices_no_devices">No paired Bluetooth devices</string>
<string name="settings_bluetooth_selected_bluetooth_devices_forgotten_device">Forgotten device</string>
<string name="settings_advanced_options_enable_force_connect_button_title">Add Force Connect button</string>
<string name="settings_advanced_options_enable_force_connect_button_description">Adds a button in service notification to force connection to car display (needs service restart)</string>
<string name="settings_advanced_options_start_service_manually_title">Start Wi-Fi service manually</string>
<string name="settings_advanced_options_start_service_manually_description">This will start Wi-Fi service to connect Wireless Android Auto.</string>
<string name="settings_advanced_options_service_running_for_title">Service running for</string>
<string name="settings_wireless_headunit_ssid_title">Car display Wi-Fi name</string>
<string name="settings_wireless_headunit_wpa2_passphrase_title">Car display Wi-Fi password</string>
<string name="settings_wireless_note">Note: Consider if your car display is rooted it can reset hotspot settings to default (HUR/AndroidAutoConnect) every time it starts. Password must be WPA2 Personal.</string>
Expand Down
12 changes: 10 additions & 2 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
android:title="@string/settings_wireless_headunit_wpa2_passphrase_title"
app:useSimpleSummaryProvider="true"/>


<Preference
app:iconSpaceReserved="true"
android:enabled="true"
Expand All @@ -88,7 +87,16 @@
android:icon="@drawable/ic_settings_24"
android:title="@string/advanced_options"
android:key="advanced_options_category"
app:initialExpandedChildrenCount="0">
app:initialExpandedChildrenCount="1">

<ListPreference
android:key="service_running_for"
android:title="@string/settings_advanced_options_service_running_for_title"
android:persistent="true"
android:defaultValue="@string/service_running_for_default_value"
android:entries="@array/service_running_for_entries"
android:entryValues="@array/service_running_for_entry_values"
app:useSimpleSummaryProvider="true" />

<SwitchPreference
app:iconSpaceReserved="true"
Expand Down

0 comments on commit 5d176c4

Please sign in to comment.