Move to JobIntentService
This commit is contained in:
		
							parent
							
								
									405a3c6ccb
								
							
						
					
					
						commit
						0add83adeb
					
				
					 8 changed files with 125 additions and 103 deletions
				
			
		| 
						 | 
					@ -2,10 +2,6 @@
 | 
				
			||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 | 
					<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 | 
				
			||||||
    package="com.blogspot.developersu.nowol">
 | 
					    package="com.blogspot.developersu.nowol">
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    <!--
 | 
					 | 
				
			||||||
        <permission android:name="com.blogspot.developersu.nowol.nowol"
 | 
					 | 
				
			||||||
        />
 | 
					 | 
				
			||||||
    -->
 | 
					 | 
				
			||||||
    <uses-permission android:name="android.permission.INTERNET" />
 | 
					    <uses-permission android:name="android.permission.INTERNET" />
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    <application
 | 
					    <application
 | 
				
			||||||
| 
						 | 
					@ -26,9 +22,11 @@
 | 
				
			||||||
            </intent-filter>
 | 
					            </intent-filter>
 | 
				
			||||||
        </activity>
 | 
					        </activity>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        <receiver android:name=".widget.NoWolWidget">
 | 
					        <receiver android:name=".widget.NoWolWidget"
 | 
				
			||||||
 | 
					            android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
 | 
				
			||||||
            <intent-filter>
 | 
					            <intent-filter>
 | 
				
			||||||
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
 | 
					                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
 | 
				
			||||||
 | 
					                <action android:name="android.intent.action.BOOT_COMPLETED" />
 | 
				
			||||||
            </intent-filter>
 | 
					            </intent-filter>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            <meta-data
 | 
					            <meta-data
 | 
				
			||||||
| 
						 | 
					@ -38,7 +36,7 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        <service
 | 
					        <service
 | 
				
			||||||
            android:name=".SendRequestService"
 | 
					            android:name=".SendRequestService"
 | 
				
			||||||
            android:exported="false" />
 | 
					            android:permission="android.permission.BIND_JOB_SERVICE" />
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        <activity android:name=".widget.WidgetConfigurator">
 | 
					        <activity android:name=".widget.WidgetConfigurator">
 | 
				
			||||||
            <intent-filter>
 | 
					            <intent-filter>
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -19,8 +19,6 @@
 | 
				
			||||||
package com.blogspot.developersu.nowol;
 | 
					package com.blogspot.developersu.nowol;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import android.app.Dialog;
 | 
					import android.app.Dialog;
 | 
				
			||||||
import android.appwidget.AppWidgetManager;
 | 
					 | 
				
			||||||
import android.content.ComponentName;
 | 
					 | 
				
			||||||
import android.content.Intent;
 | 
					import android.content.Intent;
 | 
				
			||||||
import android.content.SharedPreferences;
 | 
					import android.content.SharedPreferences;
 | 
				
			||||||
import android.os.Handler;
 | 
					import android.os.Handler;
 | 
				
			||||||
| 
						 | 
					@ -36,10 +34,12 @@ import android.widget.EditText;
 | 
				
			||||||
import android.widget.TextView;
 | 
					import android.widget.TextView;
 | 
				
			||||||
import androidx.appcompat.app.AppCompatActivity;
 | 
					import androidx.appcompat.app.AppCompatActivity;
 | 
				
			||||||
import androidx.appcompat.widget.Toolbar;
 | 
					import androidx.appcompat.widget.Toolbar;
 | 
				
			||||||
 | 
					import androidx.core.app.JobIntentService;
 | 
				
			||||||
import androidx.core.content.ContextCompat;
 | 
					import androidx.core.content.ContextCompat;
 | 
				
			||||||
import com.blogspot.developersu.nowol.widget.NoWolWidget;
 | 
					import com.blogspot.developersu.nowol.widget.NoWolWidget;
 | 
				
			||||||
import com.google.android.material.snackbar.Snackbar;
 | 
					import com.google.android.material.snackbar.Snackbar;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import static com.blogspot.developersu.nowol.SendRequestService.JOB_ID;
 | 
				
			||||||
import static com.blogspot.developersu.nowol.ServerReplies.*;
 | 
					import static com.blogspot.developersu.nowol.ServerReplies.*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class MainActivity extends AppCompatActivity {
 | 
					public class MainActivity extends AppCompatActivity {
 | 
				
			||||||
| 
						 | 
					@ -69,7 +69,7 @@ public class MainActivity extends AppCompatActivity {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private void doRequest(String url) {
 | 
					    private void doRequest(String url) {
 | 
				
			||||||
        SendRequestIntent.putExtra("url", url);
 | 
					        SendRequestIntent.putExtra("url", url);
 | 
				
			||||||
        startService(SendRequestIntent);
 | 
					        JobIntentService.enqueueWork(getApplicationContext(), SendRequestService.class, JOB_ID, SendRequestIntent);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private void inform(String textToShow){
 | 
					    private void inform(String textToShow){
 | 
				
			||||||
| 
						 | 
					@ -149,7 +149,7 @@ public class MainActivity extends AppCompatActivity {
 | 
				
			||||||
                statusLbl.setText(getResources().getString(R.string.statusOffline));
 | 
					                statusLbl.setText(getResources().getString(R.string.statusOffline));
 | 
				
			||||||
                statusLbl.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.colorRed));
 | 
					                statusLbl.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.colorRed));
 | 
				
			||||||
                break;
 | 
					                break;
 | 
				
			||||||
            case STATE_UNKNOWN:
 | 
					            case STATE_NO_REPLY:
 | 
				
			||||||
                statusLbl.setText(getResources().getString(R.string.noResponse));
 | 
					                statusLbl.setText(getResources().getString(R.string.noResponse));
 | 
				
			||||||
                statusLbl.setTextColor(hostAddress.getTextColors());
 | 
					                statusLbl.setTextColor(hostAddress.getTextColors());
 | 
				
			||||||
                break;
 | 
					                break;
 | 
				
			||||||
| 
						 | 
					@ -163,7 +163,7 @@ public class MainActivity extends AppCompatActivity {
 | 
				
			||||||
            case STATE_OFF:
 | 
					            case STATE_OFF:
 | 
				
			||||||
                inform(getResources().getString(R.string.statusOffline));
 | 
					                inform(getResources().getString(R.string.statusOffline));
 | 
				
			||||||
                break;
 | 
					                break;
 | 
				
			||||||
            case STATE_UNKNOWN:
 | 
					            case STATE_NO_REPLY:
 | 
				
			||||||
                inform(getResources().getString(R.string.noResponse) + hostAddress.getText().toString());
 | 
					                inform(getResources().getString(R.string.noResponse) + hostAddress.getText().toString());
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					@ -202,17 +202,7 @@ public class MainActivity extends AppCompatActivity {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /* Update widgets by sending broadcast intent */
 | 
					        /* Update widgets by sending broadcast intent */
 | 
				
			||||||
        Intent updateWidgetIntent = new Intent(this, NoWolWidget.class);
 | 
					        Intent updateWidgetIntent = new Intent(this, NoWolWidget.class);
 | 
				
			||||||
        updateWidgetIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
 | 
					        updateWidgetIntent.setAction(NoWolWidget.ACTION_APPWIDGET_REBUILD);
 | 
				
			||||||
 | 
					 | 
				
			||||||
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
 | 
					 | 
				
			||||||
        int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(this, NoWolWidget.class));
 | 
					 | 
				
			||||||
        updateWidgetIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
 | 
					 | 
				
			||||||
        sendBroadcast(updateWidgetIntent);
 | 
					        sendBroadcast(updateWidgetIntent);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Override
 | 
					 | 
				
			||||||
    protected void onDestroy() {
 | 
					 | 
				
			||||||
        super.onDestroy();
 | 
					 | 
				
			||||||
        stopService(SendRequestIntent); // just in case
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -18,12 +18,13 @@
 | 
				
			||||||
*/
 | 
					*/
 | 
				
			||||||
package com.blogspot.developersu.nowol;
 | 
					package com.blogspot.developersu.nowol;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import android.app.IntentService;
 | 
					 | 
				
			||||||
import android.appwidget.AppWidgetManager;
 | 
					import android.appwidget.AppWidgetManager;
 | 
				
			||||||
import android.content.Intent;
 | 
					import android.content.Intent;
 | 
				
			||||||
import android.os.Bundle;
 | 
					import android.os.Bundle;
 | 
				
			||||||
import android.os.ResultReceiver;
 | 
					import android.os.ResultReceiver;
 | 
				
			||||||
import android.widget.RemoteViews;
 | 
					import android.widget.RemoteViews;
 | 
				
			||||||
 | 
					import androidx.annotation.NonNull;
 | 
				
			||||||
 | 
					import androidx.core.app.JobIntentService;
 | 
				
			||||||
import androidx.core.content.ContextCompat;
 | 
					import androidx.core.content.ContextCompat;
 | 
				
			||||||
import com.android.volley.Request;
 | 
					import com.android.volley.Request;
 | 
				
			||||||
import com.android.volley.RequestQueue;
 | 
					import com.android.volley.RequestQueue;
 | 
				
			||||||
| 
						 | 
					@ -32,25 +33,23 @@ import com.android.volley.toolbox.Volley;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import static com.blogspot.developersu.nowol.ServerReplies.*;
 | 
					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;
 | 
					    private ResultReceiver resReceiver;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public SendRequestService() {
 | 
					 | 
				
			||||||
        super("MyIntentService");
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    protected void onHandleIntent(Intent intent) {
 | 
					    protected void onHandleWork(@NonNull Intent intent) {
 | 
				
			||||||
        Bundle bundle = intent.getExtras();
 | 
					        Bundle bundle = intent.getExtras();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (bundle == null)
 | 
					        if (bundle == null){
 | 
				
			||||||
            return;
 | 
					            return;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        String url = bundle.getString("url");
 | 
					        String url = bundle.getString("url");
 | 
				
			||||||
        resReceiver = bundle.getParcelable("receiver");
 | 
					        resReceiver = bundle.getParcelable("receiver");
 | 
				
			||||||
        final int appWidgetId = bundle.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
 | 
					        final int appWidgetId = bundle.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
 | 
				
			||||||
 | 
					 | 
				
			||||||
        RequestQueue queueStd = Volley.newRequestQueue(this);
 | 
					        RequestQueue queueStd = Volley.newRequestQueue(this);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        StringRequest strRequest = new StringRequest(Request.Method.GET, url, //will be 4 requests
 | 
					        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);
 | 
					                        sendData(STATE_ON, appWidgetId);
 | 
				
			||||||
                    else
 | 
					                    else
 | 
				
			||||||
                        sendData(STATE_OFF, appWidgetId);
 | 
					                        sendData(STATE_OFF, appWidgetId);
 | 
				
			||||||
                }, error -> sendData(STATE_UNKNOWN, appWidgetId));
 | 
					                }, error -> sendData(STATE_NO_REPLY, appWidgetId));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        queueStd.add(strRequest);
 | 
					        queueStd.add(strRequest);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					@ -69,11 +68,10 @@ public class SendRequestService extends IntentService {
 | 
				
			||||||
        final int redColor = ContextCompat.getColor(this, R.color.colorRed);
 | 
					        final int redColor = ContextCompat.getColor(this, R.color.colorRed);
 | 
				
			||||||
        final int orangeColor = ContextCompat.getColor(this, R.color.colorOrange);
 | 
					        final int orangeColor = ContextCompat.getColor(this, R.color.colorOrange);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // MainActivity requested
 | 
					        if (resReceiver != null){
 | 
				
			||||||
        if (widgetId == 0){
 | 
					 | 
				
			||||||
            resReceiver.send(state, null);
 | 
					            resReceiver.send(state, null);
 | 
				
			||||||
 | 
					            return; //todo: FIX
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					 | 
				
			||||||
        switch (state){
 | 
					        switch (state){
 | 
				
			||||||
           case STATE_ON:
 | 
					           case STATE_ON:
 | 
				
			||||||
               setWidgetTextColorIndication(widgetId, getResources().getString(R.string.statusOnline), greenColor);
 | 
					               setWidgetTextColorIndication(widgetId, getResources().getString(R.string.statusOnline), greenColor);
 | 
				
			||||||
| 
						 | 
					@ -81,7 +79,7 @@ public class SendRequestService extends IntentService {
 | 
				
			||||||
           case STATE_OFF:
 | 
					           case STATE_OFF:
 | 
				
			||||||
               setWidgetTextColorIndication(widgetId, getResources().getString(R.string.statusOffline), redColor);
 | 
					               setWidgetTextColorIndication(widgetId, getResources().getString(R.string.statusOffline), redColor);
 | 
				
			||||||
               break;
 | 
					               break;
 | 
				
			||||||
           case STATE_UNKNOWN:
 | 
					           case STATE_NO_REPLY:
 | 
				
			||||||
               setWidgetTextColorIndication(widgetId, getResources().getString(R.string.noResponse), orangeColor);
 | 
					               setWidgetTextColorIndication(widgetId, getResources().getString(R.string.noResponse), orangeColor);
 | 
				
			||||||
               break;
 | 
					               break;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -21,5 +21,5 @@ package com.blogspot.developersu.nowol;
 | 
				
			||||||
public class ServerReplies {
 | 
					public class ServerReplies {
 | 
				
			||||||
    public static final int STATE_ON = 1;
 | 
					    public static final int STATE_ON = 1;
 | 
				
			||||||
    public static final int STATE_OFF = 0;
 | 
					    public static final int STATE_OFF = 0;
 | 
				
			||||||
    public static final int STATE_UNKNOWN = -1;
 | 
					    public static final int STATE_NO_REPLY = -1;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -21,81 +21,114 @@ package com.blogspot.developersu.nowol.widget;
 | 
				
			||||||
import android.app.PendingIntent;
 | 
					import android.app.PendingIntent;
 | 
				
			||||||
import android.appwidget.AppWidgetManager;
 | 
					import android.appwidget.AppWidgetManager;
 | 
				
			||||||
import android.appwidget.AppWidgetProvider;
 | 
					import android.appwidget.AppWidgetProvider;
 | 
				
			||||||
 | 
					import android.content.ComponentName;
 | 
				
			||||||
import android.content.Context;
 | 
					import android.content.Context;
 | 
				
			||||||
import android.content.Intent;
 | 
					import android.content.Intent;
 | 
				
			||||||
import android.content.SharedPreferences;
 | 
					import android.content.SharedPreferences;
 | 
				
			||||||
import android.util.Log;
 | 
					 | 
				
			||||||
import android.widget.RemoteViews;
 | 
					import android.widget.RemoteViews;
 | 
				
			||||||
 | 
					import androidx.core.app.JobIntentService;
 | 
				
			||||||
import com.blogspot.developersu.nowol.R;
 | 
					import com.blogspot.developersu.nowol.R;
 | 
				
			||||||
import com.blogspot.developersu.nowol.SendRequestService;
 | 
					import com.blogspot.developersu.nowol.SendRequestService;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import static com.blogspot.developersu.nowol.SendRequestService.JOB_ID;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class NoWolWidget extends AppWidgetProvider {
 | 
					public class NoWolWidget extends AppWidgetProvider {
 | 
				
			||||||
 | 
					    public static final String ACTION_APPWIDGET_REBUILD = "com.blogspot.developersu.nowol.widget.ACTION_APPWIDGET_REBUILD";
 | 
				
			||||||
    @Override
 | 
					    public static final String ACTION_APPWIDGET_REQUEST = "com.blogspot.developersu.nowol.widget.ACTION_APPWIDGET_REQUEST";
 | 
				
			||||||
    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);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public void onEnabled(Context context) {
 | 
					    public void onEnabled(Context context) {
 | 
				
			||||||
        super.onEnabled(context);
 | 
					        super.onEnabled(context);
 | 
				
			||||||
 | 
					        setWidgetsFunctionality(context);   // Calls twice when first widget created; Calls once after boot and set all widgets
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public void onDeleted(Context context, int[] appWidgetIds) {
 | 
					    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
 | 
				
			||||||
 | 
					        super.onUpdate(context, appWidgetManager, appWidgetIds);
 | 
				
			||||||
        for (int appWidgetId : appWidgetIds) {
 | 
					        for (int appWidgetId : appWidgetIds) {
 | 
				
			||||||
            WidgetConfigurator.deleteBgColorPref(context, appWidgetId);
 | 
					            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);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
 | 
					    private static void setWidgetsFunctionality(Context context){
 | 
				
			||||||
                                int appWidgetId) {
 | 
					        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
 | 
				
			||||||
        int widgetBgColor = WidgetConfigurator.loadBgColorPref(context, appWidgetId);
 | 
					        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);
 | 
					        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);
 | 
					            remoteViews.setInt(R.id.widgetBodyLayout, "setBackgroundColor", widgetBgColor);
 | 
				
			||||||
            //
 | 
					            //
 | 
				
			||||||
            SharedPreferences sharedSettings = context.getSharedPreferences("NoWolPreferences", Context.MODE_PRIVATE);
 | 
					            SharedPreferences sharedSettings = context.getSharedPreferences("NoWolPreferences", Context.MODE_PRIVATE);
 | 
				
			||||||
            String hostIP = sharedSettings.getString("Host", context.getResources().getString(R.string.hostNameDefault));
 | 
					            String hostIP = sharedSettings.getString("Host", context.getResources().getString(R.string.hostNameDefault));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            remoteViews.setOnClickPendingIntent(R.id.widgetBntRefresh,
 | 
					            remoteViews.setOnClickPendingIntent(R.id.widgetBntRefresh,
 | 
				
			||||||
                createPendingIntent(context, appWidgetId, hostIP, appWidgetId+1)); // Refresh
 | 
					                    createPendingIntent(context, appWidgetId, hostIP, appWidgetId)); // Refresh
 | 
				
			||||||
            remoteViews.setOnClickPendingIntent(R.id.widgetBtnReset,
 | 
					            remoteViews.setOnClickPendingIntent(R.id.widgetBtnReset,
 | 
				
			||||||
                createPendingIntent(context, appWidgetId, hostIP + "/?RESET=on", appWidgetId+2));
 | 
					                    createPendingIntent(context, appWidgetId, hostIP + "/?RESET=on", appWidgetId+"RESET".hashCode()));
 | 
				
			||||||
            remoteViews.setOnClickPendingIntent(R.id.widgetBtnPwr,
 | 
					            remoteViews.setOnClickPendingIntent(R.id.widgetBtnPwr,
 | 
				
			||||||
                createPendingIntent(context, appWidgetId, hostIP + "/?POWER0=on", appWidgetId+3));
 | 
					                    createPendingIntent(context, appWidgetId, hostIP + "/?POWER0=on", appWidgetId+"POWER0".hashCode()));
 | 
				
			||||||
            remoteViews.setOnClickPendingIntent(R.id.widgetBtnPwr5,
 | 
					            remoteViews.setOnClickPendingIntent(R.id.widgetBtnPwr5,
 | 
				
			||||||
                createPendingIntent(context, appWidgetId, hostIP + "/?POWER1=on", appWidgetId+4));
 | 
					                    createPendingIntent(context, appWidgetId, hostIP + "/?POWER1=on", appWidgetId+"POWER1".hashCode()));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
 | 
					            appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
 | 
				
			||||||
        // Ping serv TODO: FIX
 | 
					
 | 
				
			||||||
        Intent refreshIntent = new Intent(context, SendRequestService.class);
 | 
					            requestServerStatusFromService(context, appWidgetId);
 | 
				
			||||||
        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){
 | 
					    private static PendingIntent createPendingIntent(Context context, int widgetId, String linkAddress, int code){
 | 
				
			||||||
        Intent intent = new Intent(context, SendRequestService.class);
 | 
					        Intent intent = new Intent(context, NoWolWidget.class);
 | 
				
			||||||
 | 
					        intent.setAction(ACTION_APPWIDGET_REQUEST);
 | 
				
			||||||
        intent.putExtra("url", linkAddress);
 | 
					        intent.putExtra("url", linkAddress);
 | 
				
			||||||
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
 | 
					        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
 | 
				
			||||||
        return PendingIntent.getService(context, code, intent, PendingIntent.FLAG_UPDATE_CURRENT);
 | 
					        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);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -25,16 +25,17 @@ import android.content.Intent;
 | 
				
			||||||
import android.content.SharedPreferences;
 | 
					import android.content.SharedPreferences;
 | 
				
			||||||
import android.graphics.Color;
 | 
					import android.graphics.Color;
 | 
				
			||||||
import android.os.Bundle;
 | 
					import android.os.Bundle;
 | 
				
			||||||
import android.util.Log;
 | 
					 | 
				
			||||||
import android.widget.Button;
 | 
					import android.widget.Button;
 | 
				
			||||||
import android.widget.RemoteViews;
 | 
					import android.widget.RemoteViews;
 | 
				
			||||||
import android.widget.SeekBar;
 | 
					import android.widget.SeekBar;
 | 
				
			||||||
import android.widget.TextView;
 | 
					import android.widget.TextView;
 | 
				
			||||||
import androidx.appcompat.app.AppCompatActivity;
 | 
					import androidx.appcompat.app.AppCompatActivity;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import com.blogspot.developersu.nowol.R;
 | 
					import com.blogspot.developersu.nowol.R;
 | 
				
			||||||
import com.blogspot.developersu.nowol.SendRequestService;
 | 
					 | 
				
			||||||
import com.google.android.material.switchmaterial.SwitchMaterial;
 | 
					import com.google.android.material.switchmaterial.SwitchMaterial;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.UUID;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class WidgetConfigurator extends AppCompatActivity {
 | 
					public class WidgetConfigurator extends AppCompatActivity {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private SeekBar opacityBar;
 | 
					    private SeekBar opacityBar;
 | 
				
			||||||
| 
						 | 
					@ -95,13 +96,13 @@ public class WidgetConfigurator extends AppCompatActivity {
 | 
				
			||||||
        SharedPreferences.Editor settingsEditor = sharedSettings.edit();
 | 
					        SharedPreferences.Editor settingsEditor = sharedSettings.edit();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        remoteViews.setOnClickPendingIntent(R.id.widgetBntRefresh,
 | 
					        remoteViews.setOnClickPendingIntent(R.id.widgetBntRefresh,
 | 
				
			||||||
                createPendingIntent(hostIP, appWidgetId+1)); // Refresh
 | 
					                createPendingIntent(hostIP, appWidgetId)); // Refresh
 | 
				
			||||||
        remoteViews.setOnClickPendingIntent(R.id.widgetBtnReset,
 | 
					        remoteViews.setOnClickPendingIntent(R.id.widgetBtnReset,
 | 
				
			||||||
                createPendingIntent(hostIP + "/?RESET=on", appWidgetId+2));
 | 
					                createPendingIntent(hostIP + "/?RESET=on", appWidgetId+"RESET".hashCode()));
 | 
				
			||||||
        remoteViews.setOnClickPendingIntent(R.id.widgetBtnPwr,
 | 
					        remoteViews.setOnClickPendingIntent(R.id.widgetBtnPwr,
 | 
				
			||||||
                createPendingIntent(hostIP + "/?POWER0=on", appWidgetId+3));
 | 
					                createPendingIntent(hostIP + "/?POWER0=on", appWidgetId+"POWER0".hashCode()));
 | 
				
			||||||
        remoteViews.setOnClickPendingIntent(R.id.widgetBtnPwr5,
 | 
					        remoteViews.setOnClickPendingIntent(R.id.widgetBtnPwr5,
 | 
				
			||||||
                createPendingIntent(hostIP + "/?POWER1=on", appWidgetId+4));
 | 
					                createPendingIntent(hostIP + "/?POWER1=on", appWidgetId+"POWER1".hashCode()));
 | 
				
			||||||
        int bgColor = getBackgoundColor();
 | 
					        int bgColor = getBackgoundColor();
 | 
				
			||||||
        remoteViews.setInt(R.id.widgetBodyLayout, "setBackgroundColor", bgColor);
 | 
					        remoteViews.setInt(R.id.widgetBodyLayout, "setBackgroundColor", bgColor);
 | 
				
			||||||
        settingsEditor.putInt("WidgetBgColor" + appWidgetId, bgColor).apply();
 | 
					        settingsEditor.putInt("WidgetBgColor" + appWidgetId, bgColor).apply();
 | 
				
			||||||
| 
						 | 
					@ -115,10 +116,11 @@ public class WidgetConfigurator extends AppCompatActivity {
 | 
				
			||||||
        finish();
 | 
					        finish();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    private PendingIntent createPendingIntent(String linkAddress, int code){
 | 
					    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("url", linkAddress);
 | 
				
			||||||
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
 | 
					        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(){
 | 
					    private int getBackgoundColor(){
 | 
				
			||||||
        if (bgColorSwitch.isChecked())
 | 
					        if (bgColorSwitch.isChecked())
 | 
				
			||||||
| 
						 | 
					@ -127,7 +129,7 @@ public class WidgetConfigurator extends AppCompatActivity {
 | 
				
			||||||
            return Color.argb(255-opacityBar.getProgress()*255/100, 0x00,0x00,0x00);
 | 
					            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);
 | 
					        SharedPreferences prefs = context.getSharedPreferences("NoWolPreferences", Context.MODE_PRIVATE);
 | 
				
			||||||
        return  prefs.getInt("WidgetBgColor" + appWidgetId, Color.argb(255, 0x00,0x00,0x00));
 | 
					        return  prefs.getInt("WidgetBgColor" + appWidgetId, Color.argb(255, 0x00,0x00,0x00));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -8,7 +8,7 @@
 | 
				
			||||||
        android:id="@+id/widgetHeaderLayout"
 | 
					        android:id="@+id/widgetHeaderLayout"
 | 
				
			||||||
        android:layout_width="match_parent"
 | 
					        android:layout_width="match_parent"
 | 
				
			||||||
        android:layout_height="wrap_content"
 | 
					        android:layout_height="wrap_content"
 | 
				
			||||||
        android:background="@color/colorOrange"
 | 
					        android:background="@color/colorGrey"
 | 
				
			||||||
        android:orientation="horizontal"
 | 
					        android:orientation="horizontal"
 | 
				
			||||||
        android:paddingLeft="@dimen/widget_margin"
 | 
					        android:paddingLeft="@dimen/widget_margin"
 | 
				
			||||||
        android:paddingRight="@dimen/widget_margin"
 | 
					        android:paddingRight="@dimen/widget_margin"
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2,6 +2,7 @@
 | 
				
			||||||
<resources>
 | 
					<resources>
 | 
				
			||||||
    <color name="colorPrimary">#3db535</color>
 | 
					    <color name="colorPrimary">#3db535</color>
 | 
				
			||||||
    <color name="colorPrimaryDark">#37a12f</color>
 | 
					    <color name="colorPrimaryDark">#37a12f</color>
 | 
				
			||||||
 | 
					    <color name="colorGrey">#97c8c6</color>
 | 
				
			||||||
    <color name="colorOrange">#f17a0a</color>
 | 
					    <color name="colorOrange">#f17a0a</color>
 | 
				
			||||||
    <color name="colorRed">#c20003</color>
 | 
					    <color name="colorRed">#c20003</color>
 | 
				
			||||||
    <color name="colorAccent">#ff4081</color>
 | 
					    <color name="colorAccent">#ff4081</color>
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue