Register implicit broadcasts at runtime for updating widgets

It resolves Broadcast Limitations issue if target API level is changed
as 26 or higher later.
It includes fixing code style also.

Test: Check messaging widgets' changes.

Change-Id: I6e08027b1d6a5a19cfd17f8ec3e9a895dbb3c44a
Signed-off-by: Taesu Lee <taesu82.lee@samsung.com>
This commit is contained in:
Taesu Lee
2020-02-10 15:20:27 +09:00
parent e8827131fc
commit 711bed5c23
2 changed files with 49 additions and 49 deletions

View File

@@ -484,9 +484,6 @@
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<intent-filter>
<action android:name="com.android.Bugle.intent.action.ACTION_NOTIFY_CONVERSATIONS_CHANGED" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widget_conversation_list" />
</receiver>
@@ -497,9 +494,6 @@
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<intent-filter>
<action android:name="com.android.Bugle.intent.action.ACTION_NOTIFY_MESSAGES_CHANGED" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widget_conversation" />
</receiver>

View File

@@ -21,6 +21,7 @@ import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import com.android.messaging.util.LogUtil;
@@ -78,6 +79,12 @@ public abstract class BaseWidgetProvider extends AppWidgetProvider {
}
}
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
context.getApplicationContext().registerReceiver(this, new IntentFilter(getAction()));
}
protected abstract String getAction();
protected abstract int getListId();
@@ -87,52 +94,51 @@ public abstract class BaseWidgetProvider extends AppWidgetProvider {
*/
protected abstract void updateWidget(Context context, int appWidgetId);
private int getWidgetSize(AppWidgetManager appWidgetManager,
int appWidgetId) {
if (LogUtil.isLoggable(TAG, LogUtil.VERBOSE)) {
LogUtil.v(TAG, "BaseWidgetProvider.getWidgetSize");
}
private int getWidgetSize(AppWidgetManager appWidgetManager, int appWidgetId) {
if (LogUtil.isLoggable(TAG, LogUtil.VERBOSE)) {
LogUtil.v(TAG, "BaseWidgetProvider.getWidgetSize");
}
// Get the dimensions
final Bundle options = appWidgetManager.getAppWidgetOptions(appWidgetId);
// Get the dimensions
final Bundle options = appWidgetManager.getAppWidgetOptions(appWidgetId);
// Get min width and height.
final int minWidth = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
final int minHeight = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT);
// Get min width and height.
final int minWidth = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
final int minHeight = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT);
// First find out rows and columns based on width provided.
final int rows = getCellsForSize(minHeight);
final int columns = getCellsForSize(minWidth);
if (LogUtil.isLoggable(TAG, LogUtil.VERBOSE)) {
LogUtil.v(TAG, "BaseWidgetProvider.getWidgetSize row: " + rows +
" columns: " + columns);
}
int size = SIZE_MEDIUM;
if (rows == 1) {
size = SIZE_SMALL; // Our widget doesn't let itself get this small. Perhaps in the
// future will add a super-mini widget.
} else if (columns > 3) {
size = SIZE_LARGE;
}
// put the size in the bundle so our service know what size it's dealing with.
final int savedSize = options.getInt(WIDGET_SIZE_KEY);
if (savedSize != size) {
options.putInt(WIDGET_SIZE_KEY, size);
appWidgetManager.updateAppWidgetOptions(appWidgetId, options);
// The size changed. We have to force the widget to rebuild the list.
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, getListId());
// First find out rows and columns based on width provided.
final int rows = getCellsForSize(minHeight);
final int columns = getCellsForSize(minWidth);
if (LogUtil.isLoggable(TAG, LogUtil.VERBOSE)) {
LogUtil.v(TAG, "BaseWidgetProvider.getWidgetSize old size: " + savedSize +
" new size saved: " + size);
LogUtil.v(TAG, "BaseWidgetProvider.getWidgetSize row: " + rows
+ " columns: " + columns);
}
}
return size;
int size = SIZE_MEDIUM;
if (rows == 1) {
size = SIZE_SMALL; // Our widget doesn't let itself get this small. Perhaps in the
// future will add a super-mini widget.
} else if (columns > 3) {
size = SIZE_LARGE;
}
// put the size in the bundle so our service know what size it's dealing with.
final int savedSize = options.getInt(WIDGET_SIZE_KEY);
if (savedSize != size) {
options.putInt(WIDGET_SIZE_KEY, size);
appWidgetManager.updateAppWidgetOptions(appWidgetId, options);
// The size changed. We have to force the widget to rebuild the list.
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, getListId());
if (LogUtil.isLoggable(TAG, LogUtil.VERBOSE)) {
LogUtil.v(TAG, "BaseWidgetProvider.getWidgetSize old size: " + savedSize
+ " new size saved: " + size);
}
}
return size;
}
/**
@@ -142,10 +148,10 @@ public abstract class BaseWidgetProvider extends AppWidgetProvider {
* @return Size in number of cells.
*/
private static int getCellsForSize(int size) {
// The hardwired sizes in this function come from the hardwired formula found in
// Android's UI guidelines for widget design:
// http://developer.android.com/guide/practices/ui_guidelines/widget_design.html
return (size + 30) / 70;
// The hardwired sizes in this function come from the hardwired formula found in
// Android's UI guidelines for widget design:
// http://developer.android.com/guide/practices/ui_guidelines/widget_design.html
return (size + 30) / 70;
}
@Override