Move to JobIntentService

master
Dmitry Isaenko 2020-10-04 12:03:56 +03:00
parent 405a3c6ccb
commit 0add83adeb
8 changed files with 125 additions and 103 deletions

View File

@ -2,10 +2,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.blogspot.developersu.nowol">
<!--
<permission android:name="com.blogspot.developersu.nowol.nowol"
/>
-->
<uses-permission android:name="android.permission.INTERNET" />
<application
@ -26,9 +22,11 @@
</intent-filter>
</activity>
<receiver android:name=".widget.NoWolWidget">
<receiver android:name=".widget.NoWolWidget"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<meta-data
@ -38,7 +36,7 @@
<service
android:name=".SendRequestService"
android:exported="false" />
android:permission="android.permission.BIND_JOB_SERVICE" />
<activity android:name=".widget.WidgetConfigurator">
<intent-filter>

View File

@ -19,8 +19,6 @@
package com.blogspot.developersu.nowol;
import android.app.Dialog;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
@ -36,10 +34,12 @@ import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.JobIntentService;
import androidx.core.content.ContextCompat;
import com.blogspot.developersu.nowol.widget.NoWolWidget;
import com.google.android.material.snackbar.Snackbar;
import static com.blogspot.developersu.nowol.SendRequestService.JOB_ID;
import static com.blogspot.developersu.nowol.ServerReplies.*;
public class MainActivity extends AppCompatActivity {
@ -69,7 +69,7 @@ public class MainActivity extends AppCompatActivity {
private void doRequest(String url) {
SendRequestIntent.putExtra("url", url);
startService(SendRequestIntent);
JobIntentService.enqueueWork(getApplicationContext(), SendRequestService.class, JOB_ID, SendRequestIntent);
}
private void inform(String textToShow){
@ -149,7 +149,7 @@ public class MainActivity extends AppCompatActivity {
statusLbl.setText(getResources().getString(R.string.statusOffline));
statusLbl.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.colorRed));
break;
case STATE_UNKNOWN:
case STATE_NO_REPLY:
statusLbl.setText(getResources().getString(R.string.noResponse));
statusLbl.setTextColor(hostAddress.getTextColors());
break;
@ -163,7 +163,7 @@ public class MainActivity extends AppCompatActivity {
case STATE_OFF:
inform(getResources().getString(R.string.statusOffline));
break;
case STATE_UNKNOWN:
case STATE_NO_REPLY:
inform(getResources().getString(R.string.noResponse) + hostAddress.getText().toString());
}
}
@ -202,17 +202,7 @@ public class MainActivity extends AppCompatActivity {
/* Update widgets by sending broadcast intent */
Intent updateWidgetIntent = new Intent(this, NoWolWidget.class);
updateWidgetIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(this, NoWolWidget.class));
updateWidgetIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
updateWidgetIntent.setAction(NoWolWidget.ACTION_APPWIDGET_REBUILD);
sendBroadcast(updateWidgetIntent);
}
@Override
protected void onDestroy() {
super.onDestroy();
stopService(SendRequestIntent); // just in case
}
}

View File

@ -18,12 +18,13 @@
*/
package com.blogspot.developersu.nowol;
import android.app.IntentService;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.os.Bundle;
import android.os.ResultReceiver;
import android.widget.RemoteViews;
import androidx.annotation.NonNull;
import androidx.core.app.JobIntentService;
import androidx.core.content.ContextCompat;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
@ -32,25 +33,23 @@ import com.android.volley.toolbox.Volley;
import static com.blogspot.developersu.nowol.ServerReplies.*;
public class SendRequestService extends IntentService {
public class SendRequestService extends JobIntentService {
public static final int JOB_ID = 1;
private ResultReceiver resReceiver;
public SendRequestService() {
super("MyIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
protected void onHandleWork(@NonNull Intent intent) {
Bundle bundle = intent.getExtras();
if (bundle == null)
if (bundle == null){
return;
}
String url = bundle.getString("url");
resReceiver = bundle.getParcelable("receiver");
final int appWidgetId = bundle.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
RequestQueue queueStd = Volley.newRequestQueue(this);
StringRequest strRequest = new StringRequest(Request.Method.GET, url, //will be 4 requests
@ -59,7 +58,7 @@ public class SendRequestService extends IntentService {
sendData(STATE_ON, appWidgetId);
else
sendData(STATE_OFF, appWidgetId);
}, error -> sendData(STATE_UNKNOWN, appWidgetId));
}, error -> sendData(STATE_NO_REPLY, appWidgetId));
queueStd.add(strRequest);
}
@ -69,11 +68,10 @@ public class SendRequestService extends IntentService {
final int redColor = ContextCompat.getColor(this, R.color.colorRed);
final int orangeColor = ContextCompat.getColor(this, R.color.colorOrange);
// MainActivity requested
if (widgetId == 0){
if (resReceiver != null){
resReceiver.send(state, null);
return; //todo: FIX
}
switch (state){
case STATE_ON:
setWidgetTextColorIndication(widgetId, getResources().getString(R.string.statusOnline), greenColor);
@ -81,7 +79,7 @@ public class SendRequestService extends IntentService {
case STATE_OFF:
setWidgetTextColorIndication(widgetId, getResources().getString(R.string.statusOffline), redColor);
break;
case STATE_UNKNOWN:
case STATE_NO_REPLY:
setWidgetTextColorIndication(widgetId, getResources().getString(R.string.noResponse), orangeColor);
break;
}

View File

@ -21,5 +21,5 @@ package com.blogspot.developersu.nowol;
public class ServerReplies {
public static final int STATE_ON = 1;
public static final int STATE_OFF = 0;
public static final int STATE_UNKNOWN = -1;
public static final int STATE_NO_REPLY = -1;
}

View File

@ -21,81 +21,114 @@ package com.blogspot.developersu.nowol.widget;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.RemoteViews;
import androidx.core.app.JobIntentService;
import com.blogspot.developersu.nowol.R;
import com.blogspot.developersu.nowol.SendRequestService;
import static com.blogspot.developersu.nowol.SendRequestService.JOB_ID;
public class NoWolWidget extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.hasExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS)){
int[] widgetIDs = intent.getExtras().getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);
for (int widgetID : widgetIDs){
updateAppWidget(context, AppWidgetManager.getInstance(context), widgetID);
}
}
else
super.onReceive(context, intent);
}
public static final String ACTION_APPWIDGET_REBUILD = "com.blogspot.developersu.nowol.widget.ACTION_APPWIDGET_REBUILD";
public static final String ACTION_APPWIDGET_REQUEST = "com.blogspot.developersu.nowol.widget.ACTION_APPWIDGET_REQUEST";
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
setWidgetsFunctionality(context); // Calls twice when first widget created; Calls once after boot and set all widgets
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
for (int appWidgetId : appWidgetIds) {
requestServerStatusFromService(context, appWidgetId);
}
}
private static void requestServerStatusFromService(Context context, int appWidgetId) {
SharedPreferences sharedSettings = context.getSharedPreferences("NoWolPreferences", Context.MODE_PRIVATE);
String hostIP = sharedSettings.getString("Host", context.getResources().getString(R.string.hostNameDefault));
requestServerStatusFromServiceByIp(context, appWidgetId, hostIP);
}
private static void requestServerStatusFromServiceByIp(Context context, int appWidgetId, String hostIP) {
// Ping server
Intent refreshIntent = new Intent(context, SendRequestService.class);
refreshIntent.putExtra("url", hostIP);
refreshIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
JobIntentService.enqueueWork(context, SendRequestService.class, JOB_ID, refreshIntent);
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (intent.getAction() == null)
return;
switch (intent.getAction()){
case ACTION_APPWIDGET_REBUILD:
setWidgetsFunctionality(context);
break;
case ACTION_APPWIDGET_REQUEST:
int widgetID = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
String host = intent.getExtras().getString("url");
if (widgetID != 0){
requestServerStatusFromServiceByIp(context, widgetID, host);
}
}
}
private static void setWidgetsFunctionality(Context context){
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, NoWolWidget.class));
setWidgetsFunctionality(context, appWidgetIds);
}
private static void setWidgetsFunctionality(Context context, int[] appWidgetIds){
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.no_wol_widget);
for (int appWidgetId : appWidgetIds){
int widgetBgColor = WidgetConfigurator.loadBgColorPref(context, appWidgetId);
remoteViews.setInt(R.id.widgetBodyLayout, "setBackgroundColor", widgetBgColor);
//
SharedPreferences sharedSettings = context.getSharedPreferences("NoWolPreferences", Context.MODE_PRIVATE);
String hostIP = sharedSettings.getString("Host", context.getResources().getString(R.string.hostNameDefault));
remoteViews.setOnClickPendingIntent(R.id.widgetBntRefresh,
createPendingIntent(context, appWidgetId, hostIP, appWidgetId)); // Refresh
remoteViews.setOnClickPendingIntent(R.id.widgetBtnReset,
createPendingIntent(context, appWidgetId, hostIP + "/?RESET=on", appWidgetId+"RESET".hashCode()));
remoteViews.setOnClickPendingIntent(R.id.widgetBtnPwr,
createPendingIntent(context, appWidgetId, hostIP + "/?POWER0=on", appWidgetId+"POWER0".hashCode()));
remoteViews.setOnClickPendingIntent(R.id.widgetBtnPwr5,
createPendingIntent(context, appWidgetId, hostIP + "/?POWER1=on", appWidgetId+"POWER1".hashCode()));
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
requestServerStatusFromService(context, appWidgetId);
}
}
private static PendingIntent createPendingIntent(Context context, int widgetId, String linkAddress, int code){
Intent intent = new Intent(context, NoWolWidget.class);
intent.setAction(ACTION_APPWIDGET_REQUEST);
intent.putExtra("url", linkAddress);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
return PendingIntent.getBroadcast(context, code, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
for (int appWidgetId : appWidgetIds) {
WidgetConfigurator.deleteBgColorPref(context, appWidgetId);
}
}
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
int widgetBgColor = WidgetConfigurator.loadBgColorPref(context, appWidgetId);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.no_wol_widget);
remoteViews.setInt(R.id.widgetBodyLayout, "setBackgroundColor", widgetBgColor);
//
SharedPreferences sharedSettings = context.getSharedPreferences("NoWolPreferences", Context.MODE_PRIVATE);
String hostIP = sharedSettings.getString("Host", context.getResources().getString(R.string.hostNameDefault));
remoteViews.setOnClickPendingIntent(R.id.widgetBntRefresh,
createPendingIntent(context, appWidgetId, hostIP, appWidgetId+1)); // Refresh
remoteViews.setOnClickPendingIntent(R.id.widgetBtnReset,
createPendingIntent(context, appWidgetId, hostIP + "/?RESET=on", appWidgetId+2));
remoteViews.setOnClickPendingIntent(R.id.widgetBtnPwr,
createPendingIntent(context, appWidgetId, hostIP + "/?POWER0=on", appWidgetId+3));
remoteViews.setOnClickPendingIntent(R.id.widgetBtnPwr5,
createPendingIntent(context, appWidgetId, hostIP + "/?POWER1=on", appWidgetId+4));
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
// Ping serv TODO: FIX
Intent refreshIntent = new Intent(context, SendRequestService.class);
refreshIntent.putExtra("url", hostIP);
refreshIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
context.startService(refreshIntent);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
private static PendingIntent createPendingIntent(Context context, int widgetId, String linkAddress, int code){
Intent intent = new Intent(context, SendRequestService.class);
intent.putExtra("url", linkAddress);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
return PendingIntent.getService(context, code, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
}

View File

@ -25,16 +25,17 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.RemoteViews;
import android.widget.SeekBar;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.blogspot.developersu.nowol.R;
import com.blogspot.developersu.nowol.SendRequestService;
import com.google.android.material.switchmaterial.SwitchMaterial;
import java.util.UUID;
public class WidgetConfigurator extends AppCompatActivity {
private SeekBar opacityBar;
@ -95,13 +96,13 @@ public class WidgetConfigurator extends AppCompatActivity {
SharedPreferences.Editor settingsEditor = sharedSettings.edit();
remoteViews.setOnClickPendingIntent(R.id.widgetBntRefresh,
createPendingIntent(hostIP, appWidgetId+1)); // Refresh
createPendingIntent(hostIP, appWidgetId)); // Refresh
remoteViews.setOnClickPendingIntent(R.id.widgetBtnReset,
createPendingIntent(hostIP + "/?RESET=on", appWidgetId+2));
createPendingIntent(hostIP + "/?RESET=on", appWidgetId+"RESET".hashCode()));
remoteViews.setOnClickPendingIntent(R.id.widgetBtnPwr,
createPendingIntent(hostIP + "/?POWER0=on", appWidgetId+3));
createPendingIntent(hostIP + "/?POWER0=on", appWidgetId+"POWER0".hashCode()));
remoteViews.setOnClickPendingIntent(R.id.widgetBtnPwr5,
createPendingIntent(hostIP + "/?POWER1=on", appWidgetId+4));
createPendingIntent(hostIP + "/?POWER1=on", appWidgetId+"POWER1".hashCode()));
int bgColor = getBackgoundColor();
remoteViews.setInt(R.id.widgetBodyLayout, "setBackgroundColor", bgColor);
settingsEditor.putInt("WidgetBgColor" + appWidgetId, bgColor).apply();
@ -115,10 +116,11 @@ public class WidgetConfigurator extends AppCompatActivity {
finish();
}
private PendingIntent createPendingIntent(String linkAddress, int code){
Intent intent = new Intent(context, SendRequestService.class);
Intent intent = new Intent(context, NoWolWidget.class);
intent.setAction(NoWolWidget.ACTION_APPWIDGET_REQUEST);
intent.putExtra("url", linkAddress);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
return PendingIntent.getService(context, code, intent, PendingIntent.FLAG_UPDATE_CURRENT);
return PendingIntent.getBroadcast(context, code, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
private int getBackgoundColor(){
if (bgColorSwitch.isChecked())
@ -127,7 +129,7 @@ public class WidgetConfigurator extends AppCompatActivity {
return Color.argb(255-opacityBar.getProgress()*255/100, 0x00,0x00,0x00);
}
static int loadBgColorPref(Context context, int appWidgetId) {
public static int loadBgColorPref(Context context, int appWidgetId) {
SharedPreferences prefs = context.getSharedPreferences("NoWolPreferences", Context.MODE_PRIVATE);
return prefs.getInt("WidgetBgColor" + appWidgetId, Color.argb(255, 0x00,0x00,0x00));
}

View File

@ -8,7 +8,7 @@
android:id="@+id/widgetHeaderLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorOrange"
android:background="@color/colorGrey"
android:orientation="horizontal"
android:paddingLeft="@dimen/widget_margin"
android:paddingRight="@dimen/widget_margin"

View File

@ -2,6 +2,7 @@
<resources>
<color name="colorPrimary">#3db535</color>
<color name="colorPrimaryDark">#37a12f</color>
<color name="colorGrey">#97c8c6</color>
<color name="colorOrange">#f17a0a</color>
<color name="colorRed">#c20003</color>
<color name="colorAccent">#ff4081</color>