0

I have a Flashlight widget that stops responding after the launcher is restarted, for example when date is changed in system, the launcher resets on my phone. According to this thread on SO: Link, I need to update the remoteView instead of creating new one every time. However I don't understand how to implement this in my code. I have a provider and a receiver for this.

Note: The widget starts working again after 30 mins as that is the time set for my widget to update in my appwidget-provider XML.

Provider:

public class WidgetProvider extends AppWidgetProvider {
   @Override
  public void onUpdate(Context context, AppWidgetManager appWidgetManager,
    int[] appWidgetIds) {
    final int N = appWidgetIds.length;
    for (int i = 0; i < N; i++) {
      int appWidgetId = appWidgetIds[i];

      Intent receiver = new Intent(context, FlashlightWidgetReceiver.class);
      receiver.setAction("COM_FLASHLIGHT");
      receiver.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
      PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, receiver, 0);

      RemoteViews views = new RemoteViews(context.getPackageName(),
        R.layout.appwidget_layout);
      views.setOnClickPendingIntent(R.id.imageButton, pendingIntent);
      //appWidgetManager.updateAppWidget(appWidgetId, views);
      appWidgetManager.partiallyUpdateAppWidget(appWidgetId, views);
    }

  }
}

Receiver:

public class FlashlightWidgetReceiver extends BroadcastReceiver {
  private static boolean isLightOn = false;
  private static Camera camera;

  @Override
  public void onReceive(Context context, Intent intent) {
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_layout);

    if (isLightOn) {
      views.setImageViewResource(R.id.imageButton, R.drawable.btn_switch_off);
    } else {
      views.setImageViewResource(R.id.imageButton, R.drawable.btn_switch_on);
    }

    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    appWidgetManager.updateAppWidget(new ComponentName(context, WidgetProvider.class),
      views);

    if (isLightOn) {
      if (camera != null) {
        camera.stopPreview();
        camera.release();
        camera = null;
        isLightOn = false;
      }

    } else {
      // Open the default i.e. the first rear facing camera.
      camera = Camera.open();

      if (camera == null) {
        Toast.makeText(context, "No Camera!", Toast.LENGTH_SHORT).show();
      } else {
        // Set the torch flash mode
        Camera.Parameters param = camera.getParameters();
        param.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
        try {
          camera.setParameters(param);
          camera.startPreview();
          isLightOn = true;
        } catch (Exception e) {
          Toast.makeText(context, "No Camera!", Toast.LENGTH_SHORT).show();
        }
      }
    }
  }
}
Community
  • 1
  • 1
Muhammad Ali
  • 3,478
  • 5
  • 19
  • 30

2 Answers2

0

I had this problem for one of my widgets and the way I solved it was to store a static reference to the RemoteViews object. So I added a class to associate each appWidgetId with it's RemoteViews object:

public class WidgetInfo {
    int appWidgetId;
    RemoteViews view;
    boolean clickHandlersIntialized;

    public WidgetInfo(int id, RemoteViews v){
        appWidgetId = id;
        view = v;
        clickHandlersInitialized = false;
    }
}

Then created a static list of these objects in the provider:

private static ArrayList<WidgetInfo> widgetList = new ArrayList<WidgetInfo>();

Then created this method in the provider, which given an appWidgetId will return the corresponding WidgetInfo object

public WidgetInfo getInfo(Context context, int id) {
    for (int i = 0; i < widgetList.size(); i++) {
        if (widgetList.get(i).appWidgetId == id) {
            // if there is already a WidgetInfo object in the list
            // with this id, return the object
            return widgetList.get(i);
        }
    }
    // otherwise, create a new WidgetInfo object with a new RemoteViews
    // and add it to the list
    WidgetInfo newInfo = new WidgetInfo(id, new RemoteViews(context.getPackageName(), R.layout.appwidget_layout));
    widgetList.add(newInfo);
    return newInfo;
}

Then in the onUpdate method of the provider, you can use getInfo to get the WidgetInfo object, and then call methods on the existing view member. For example:

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
    final int N = appWidgetIds.length;
    for (int i = 0; i < N; i++) {
      int appWidgetId = appWidgetIds[i];

      WidgetInfo info = getInfo(context, appWidgetId);
      RemoteViews views = info.view; 
      // this prevents the click handlers from being initialized multiple times
      if(!info.clickHandlersInitialized){
          Intent receiver = new Intent(context, FlashlightWidgetReceiver.class);
          receiver.setAction("COM_FLASHLIGHT");
          receiver.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
          PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, receiver, 0);
          views.setOnClickPendingIntent(R.id.imageButton, pendingIntent);
          info.clickHandlersInitialized = true;
      }

      //appWidgetManager.updateAppWidget(appWidgetId, views);
      appWidgetManager.partiallyUpdateAppWidget(appWidgetId, views);
    }
}
mpallansch
  • 1,174
  • 6
  • 16
  • +1 for logic, although I was able to solve it before this post so i'm not sure if it works! Thank you though, I will look into this for my next widget :) – Muhammad Ali May 23 '15 at 01:27
  • It is a very bad idea to make static remoteViews, or whatsover to reuse it; each time you add something to the remoteviews you are accumulating all the previous instructions until you get an Out of Memory error. – htafoya Sep 08 '15 at 02:09
0

Alright guys, I finally got time to fix this problem once and for all :)

I created more methods for the provider instead of doing everything in onUpdate, one important method needed:

 public static PendingIntent buildButtonPendingIntent(Context context) {
   Intent intent = new Intent();
   intent.setAction("COM_FLASHLIGHT");
   return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
 }

And this method is called through the receiver when the widget is clicked using the code below:

private void turnFlash(Context context) {
  RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_layout);
  views.setOnClickPendingIntent(R.id.imageButton, WidgetProvider.buildButtonPendingIntent(context));
}

That is all, no more hiccups!

Muhammad Ali
  • 3,478
  • 5
  • 19
  • 30