create utility method for sending Toasts from Services
This commit is contained in:
		
							parent
							
								
									0322e87d18
								
							
						
					
					
						commit
						43d3653753
					
				@ -19,7 +19,6 @@
 | 
			
		||||
package org.fdroid.fdroid;
 | 
			
		||||
 | 
			
		||||
import android.app.AlarmManager;
 | 
			
		||||
import android.app.IntentService;
 | 
			
		||||
import android.app.NotificationManager;
 | 
			
		||||
import android.app.PendingIntent;
 | 
			
		||||
import android.app.job.JobInfo;
 | 
			
		||||
@ -33,8 +32,6 @@ import android.content.IntentFilter;
 | 
			
		||||
import android.net.Uri;
 | 
			
		||||
import android.os.AsyncTask;
 | 
			
		||||
import android.os.Build;
 | 
			
		||||
import android.os.Handler;
 | 
			
		||||
import android.os.Looper;
 | 
			
		||||
import android.os.Process;
 | 
			
		||||
import android.os.SystemClock;
 | 
			
		||||
import android.support.annotation.NonNull;
 | 
			
		||||
@ -87,7 +84,6 @@ public class UpdateService extends JobIntentService {
 | 
			
		||||
    private static final int NOTIFY_ID_UPDATING = 0;
 | 
			
		||||
 | 
			
		||||
    private static UpdateService updateService;
 | 
			
		||||
    private static Handler toastHandler;
 | 
			
		||||
 | 
			
		||||
    private NotificationManager notificationManager;
 | 
			
		||||
    private NotificationCompat.Builder notificationBuilder;
 | 
			
		||||
@ -391,22 +387,6 @@ public class UpdateService extends JobIntentService {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * In order to send a {@link Toast} from a {@link IntentService}, we have to do these tricks.
 | 
			
		||||
     */
 | 
			
		||||
    private void sendNoInternetToast() {
 | 
			
		||||
        if (toastHandler == null) {
 | 
			
		||||
            toastHandler = new Handler(Looper.getMainLooper());
 | 
			
		||||
        }
 | 
			
		||||
        toastHandler.post(new Runnable() {
 | 
			
		||||
            @Override
 | 
			
		||||
            public void run() {
 | 
			
		||||
                Toast.makeText(getApplicationContext(),
 | 
			
		||||
                        R.string.warning_no_internet, Toast.LENGTH_SHORT).show();
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static boolean isLocalRepoAddress(String address) {
 | 
			
		||||
        return address != null &&
 | 
			
		||||
                (address.startsWith(BluetoothDownloader.SCHEME)
 | 
			
		||||
@ -453,7 +433,7 @@ public class UpdateService extends JobIntentService {
 | 
			
		||||
                if (!foundLocalRepo) {
 | 
			
		||||
                    Utils.debugLog(TAG, "No internet, cannot update");
 | 
			
		||||
                    if (manualUpdate) {
 | 
			
		||||
                        sendNoInternetToast();
 | 
			
		||||
                        Utils.showToastFromService(this, getString(R.string.warning_no_internet), Toast.LENGTH_SHORT);
 | 
			
		||||
                    }
 | 
			
		||||
                    return;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
@ -27,6 +27,8 @@ import android.database.Cursor;
 | 
			
		||||
import android.graphics.Bitmap;
 | 
			
		||||
import android.net.Uri;
 | 
			
		||||
import android.os.Build;
 | 
			
		||||
import android.os.Handler;
 | 
			
		||||
import android.os.Looper;
 | 
			
		||||
import android.os.StatFs;
 | 
			
		||||
import android.support.annotation.NonNull;
 | 
			
		||||
import android.support.annotation.Nullable;
 | 
			
		||||
@ -42,6 +44,7 @@ import android.text.style.TypefaceSpan;
 | 
			
		||||
import android.util.DisplayMetrics;
 | 
			
		||||
import android.util.Log;
 | 
			
		||||
import android.util.TypedValue;
 | 
			
		||||
import android.widget.Toast;
 | 
			
		||||
import com.nostra13.universalimageloader.core.DisplayImageOptions;
 | 
			
		||||
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
 | 
			
		||||
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
 | 
			
		||||
@ -101,6 +104,8 @@ public final class Utils {
 | 
			
		||||
 | 
			
		||||
    private static Pattern safePackageNamePattern;
 | 
			
		||||
 | 
			
		||||
    private static Handler toastHandler;
 | 
			
		||||
 | 
			
		||||
    public static final String FALLBACK_ICONS_DIR = "/icons/";
 | 
			
		||||
 | 
			
		||||
    /*
 | 
			
		||||
@ -840,4 +845,21 @@ public final class Utils {
 | 
			
		||||
            Utils.debugLog(logTag, "[" + duration + "ms] " + message);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * In order to send a {@link Toast} from a {@link android.app.Service}, we
 | 
			
		||||
     * have to do these tricks.
 | 
			
		||||
     */
 | 
			
		||||
    public static void showToastFromService(final Context context, final String msg, final int length) {
 | 
			
		||||
        if (toastHandler == null) {
 | 
			
		||||
            toastHandler = new Handler(Looper.getMainLooper());
 | 
			
		||||
        }
 | 
			
		||||
        toastHandler.post(new Runnable() {
 | 
			
		||||
 | 
			
		||||
            @Override
 | 
			
		||||
            public void run() {
 | 
			
		||||
                Toast.makeText(context.getApplicationContext(), msg, length).show();
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user