Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
android:xlargeScreens="true" />

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
Expand Down Expand Up @@ -315,6 +316,12 @@
android:name="com.farmerbb.secondscreen.service.TurnOffService"
android:exported="true" >
</service>
<receiver android:name="com.farmerbb.secondscreen.service.SecondScreenIntentService$NetworkStateReceiver"
android:exported="true" android:enabled="false">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,24 @@
// Receiver run by Tasker periodically to check the state of currently active profiles, whenever
// a SecondScreen state is included as a condition in a Tasker profile.
public final class TaskerConditionReceiver extends BroadcastReceiver {
Bundle lastbundle = null;


@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent) {
if (lastbundle.equals(intent.getBundleExtra(com.twofortyfouram.locale.api.Intent.EXTRA_BUNDLE))) {
return;
}
updateValues(intent);
if(U.isExternalAccessDisabled(context)) return;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line should be moved above the four lines that are newly added. If the external access pref is disabled then the receiver should not run anything, period.


BundleScrubber.scrub(intent);

final Bundle bundle = intent.getBundleExtra(com.twofortyfouram.locale.api.Intent.EXTRA_BUNDLE);
BundleScrubber.scrub(bundle);
BundleScrubber.scrub(lastbundle);

if(PluginBundleManager.isBundleValid(bundle)) {
String filename = bundle.getString(PluginBundleManager.BUNDLE_EXTRA_STRING_MESSAGE);
if(PluginBundleManager.isBundleValid(lastbundle)) {
String filename = lastbundle.getString(PluginBundleManager.BUNDLE_EXTRA_STRING_MESSAGE);
SharedPreferences prefCurrent = context.getSharedPreferences("current", Context.MODE_MULTI_PROCESS);

if("quick_actions".equals(prefCurrent.getString("filename", "0"))) {
Expand Down Expand Up @@ -72,4 +78,8 @@ public void onReceive(Context context, Intent intent) {
}
}
}

private void updateValues(Intent intent) {
lastbundle = intent.getBundleExtra(com.twofortyfouram.locale.api.Intent.EXTRA_BUNDLE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
package com.farmerbb.secondscreen.service;

import android.annotation.TargetApi;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Intent;
import android.app.*;
import android.content.*;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.Network;
import android.os.Build;
import android.os.IBinder;

Expand Down Expand Up @@ -54,11 +54,17 @@ public IBinder onBind(Intent intent) {
return binder;
}


@CallSuper
@Override
protected void onHandleIntent(@Nullable Intent intent) {
if(intent.getBooleanExtra("start_foreground", false))
startForeground();
if (checkNetwork()) {
if(intent.getBooleanExtra("start_foreground", false))
startForeground();
}
else {
NetworkStateReceiver.enable(getApplicationContext());
}
}

@Override
Expand Down Expand Up @@ -108,4 +114,62 @@ public Notification getNotification() {

return mBuilder.build();
}

boolean checkNetwork() {
final ConnectivityManager connManager = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
Network activeNetwork = connManager.getActiveNetwork();
if (activeNetwork != null) {
return true;
}
return false;
}

public static class NetworkStateReceiver extends BroadcastReceiver {
private static final String TAG = NetworkStateReceiver.class.getName();

private static SecondScreenIntentService service;

public static void setService(SecondScreenIntentService newService) {
service = newService;
}

@Override
public void onReceive(Context context, Intent intent) {
if (service.checkNetwork()) {
NetworkStateReceiver.disable(context);

final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

final Intent innerIntent = new Intent(context, SecondScreenIntentService.class);
final PendingIntent pendingIntent = PendingIntent.getService(context, 0, innerIntent, 0);

SharedPreferences preferences = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
preferences.edit();
boolean autoRefreshEnabled = preferences.getBoolean("pref_auto_refresh_enabled", false);

final String hours = preferences.getString("pref_auto_refresh_enabled", "0");
long hoursLong = Long.parseLong(hours) * 60 * 60 * 1000;

if (autoRefreshEnabled && hoursLong != 0) {
final long alarmTime = preferences.getLong("last_auto_refresh_time", 0) + hoursLong;
alarmManager.set(AlarmManager.RTC, alarmTime, pendingIntent);
} else {

alarmManager.cancel(pendingIntent);
}
}
}

public static void enable(Context context) {
final PackageManager packageManager = context.getPackageManager();
final ComponentName receiver = new ComponentName(context, NetworkStateReceiver.class);
packageManager.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
}

public static void disable(Context context) {
final PackageManager packageManager = context.getPackageManager();
final ComponentName receiver = new ComponentName(context, NetworkStateReceiver.class);
packageManager.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}
}
}