下面列出了android.appwidget.AppWidgetManager#getAppWidgetInfo ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static void updateWidgets(Context context, AppWidgetManager manager, int[] appWidgetIds, boolean nextAccount) {
Log.d("Financisto", "updateWidgets " + Arrays.toString(appWidgetIds) + " -> " + nextAccount);
for (int id : appWidgetIds) {
AppWidgetProviderInfo appWidgetInfo = manager.getAppWidgetInfo(id);
if (appWidgetInfo != null) {
int layoutId = appWidgetInfo.initialLayout;
if (MyPreferences.isWidgetEnabled(context)) {
long accountId = loadAccountForWidget(context, id);
Class providerClass = getProviderClass(appWidgetInfo);
Log.d("Financisto", "using provider " + providerClass);
RemoteViews remoteViews = nextAccount || accountId == -1
? buildUpdateForNextAccount(context, id, layoutId, providerClass, accountId)
: buildUpdateForCurrentAccount(context, id, layoutId, providerClass, accountId);
manager.updateAppWidget(id, remoteViews);
} else {
manager.updateAppWidget(id, noDataUpdate(context, layoutId));
}
}
}
}
public static WidgetListItem createWidgetListItem(Context context, int appWidgetId, AppWidgetManager widgetManager, SuntimesData data, String widgetTitle, String type) throws ClassNotFoundException
{
AppWidgetProviderInfo info = widgetManager.getAppWidgetInfo(appWidgetId);
String title = context.getString(R.string.configLabel_widgetList_itemTitle, widgetTitle);
String source = ((data.calculatorMode() == null) ? "def" : data.calculatorMode().getName());
String summary = context.getString(R.string.configLabel_widgetList_itemSummaryPattern, type, source);
return new WidgetListItem(appWidgetId, info.icon, title, summary, Class.forName(info.configure.getClassName()) );
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
appWidgetManager.getAppWidgetInfo(appWidgetId);
// Get the layout for the App Widget and update fields
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget_clementine);
switch (mCurrentClementineAction) {
case DEFAULT:
case CONNECTION_STATUS:
updateViewsOnConnectionStatusChange(context, views);
break;
case STATE_CHANGE:
updateViewsOnStateChange(context, views);
break;
}
// Tell the AppWidgetManager to perform an update on the current app widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
protected static int[] getWidgetSize(Context context, int appWidgetId) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(
context.getApplicationContext());
AppWidgetProviderInfo providerInfo = appWidgetManager.getAppWidgetInfo(appWidgetId);
int mWidgetLandWidth = providerInfo.minWidth;
int mWidgetPortHeight = providerInfo.minHeight;
int mWidgetPortWidth = providerInfo.minWidth;
int mWidgetLandHeight = providerInfo.minHeight;
Bundle mAppWidgetOptions = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
mAppWidgetOptions = appWidgetManager.getAppWidgetOptions(appWidgetId);
}
if (mAppWidgetOptions != null
&& mAppWidgetOptions
.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH) > 0) {
mWidgetPortWidth = mAppWidgetOptions
.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
mWidgetLandWidth = mAppWidgetOptions
.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH);
mWidgetLandHeight = mAppWidgetOptions
.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT);
mWidgetPortHeight = mAppWidgetOptions
.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT);
} else {
mWidgetLandWidth = providerInfo.minWidth;
mWidgetPortHeight = providerInfo.minHeight;
mWidgetPortWidth = providerInfo.minWidth;
mWidgetLandHeight = providerInfo.minHeight;
}
int mWidgetWidthPerOrientation = mWidgetPortWidth;
int mWidgetHeightPerOrientation = mWidgetPortHeight;
if (!isPortrait(context)) {
mWidgetWidthPerOrientation = mWidgetLandWidth;
mWidgetHeightPerOrientation = mWidgetLandHeight;
}
int[] size = new int[2];
if (AppPreference.isWidgetGraphNativeScaled(context)) {
size[0] = mWidgetWidthPerOrientation;
size[1] = mWidgetHeightPerOrientation;
return size;
}
size[0] = dipToPixels(context, mWidgetWidthPerOrientation);
size[1] = dipToPixels(context, mWidgetHeightPerOrientation);
return size;
}
/**
* Updates the app widgets whose id has changed during the restore process.
*/
static void restoreAppWidgetIds(Context context, PendingResult asyncResult,
int[] oldWidgetIds, int[] newWidgetIds) {
final ContentResolver cr = context.getContentResolver();
final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
AppWidgetHost appWidgetHost = new AppWidgetHost(context, Launcher.APPWIDGET_HOST_ID);
for (int i = 0; i < oldWidgetIds.length; i++) {
final AppWidgetProviderInfo provider = widgets.getAppWidgetInfo(newWidgetIds[i]);
final int state;
if (LauncherModel.isValidProvider(provider)) {
// This will ensure that we show 'Click to setup' UI if required.
state = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
} else {
state = LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY;
}
String[] widgetIdParams = new String[] { Integer.toString(oldWidgetIds[i]) };
int result = new ContentWriter(context, new ContentWriter.CommitParams(
"appWidgetId=? and (restored & 1) = 1", widgetIdParams))
.put(LauncherSettings.Favorites.APPWIDGET_ID, newWidgetIds[i])
.put(LauncherSettings.Favorites.RESTORED, state)
.commit();
if (result == 0) {
Cursor cursor = cr.query(Favorites.CONTENT_URI,
new String[] {Favorites.APPWIDGET_ID},
"appWidgetId=?", widgetIdParams, null);
try {
if (!cursor.moveToFirst()) {
// The widget no long exists.
appWidgetHost.deleteAppWidgetId(newWidgetIds[i]);
}
} finally {
cursor.close();
}
}
}
LauncherAppState app = LauncherAppState.getInstanceNoCreate();
if (app != null) {
app.getModel().forceReload();
}
asyncResult.finish();
}
/**
* Updates the app widgets whose id has changed during the restore process.
*/
static void restoreAppWidgetIds(Context context, int[] oldWidgetIds, int[] newWidgetIds) {
final ContentResolver cr = context.getContentResolver();
final List<Integer> idsToRemove = new ArrayList<Integer>();
final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
for (int i = 0; i < oldWidgetIds.length; i++) {
Log.i(TAG, "Widget state restore id " + oldWidgetIds[i] + " => " + newWidgetIds[i]);
final AppWidgetProviderInfo provider = widgets.getAppWidgetInfo(newWidgetIds[i]);
final int state;
if (LauncherModel.isValidProvider(provider)) {
// This will ensure that we show 'Click to setup' UI if required.
state = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
} else {
state = LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY;
}
ContentValues values = new ContentValues();
values.put(LauncherSettings.Favorites.APPWIDGET_ID, newWidgetIds[i]);
values.put(LauncherSettings.Favorites.RESTORED, state);
String[] widgetIdParams = new String[] { Integer.toString(oldWidgetIds[i]) };
int result = cr.update(Favorites.CONTENT_URI, values,
"appWidgetId=? and (restored & 1) = 1", widgetIdParams);
if (result == 0) {
Cursor cursor = cr.query(Favorites.CONTENT_URI,
new String[] {Favorites.APPWIDGET_ID},
"appWidgetId=?", widgetIdParams, null);
try {
if (!cursor.moveToFirst()) {
// The widget no long exists.
idsToRemove.add(newWidgetIds[i]);
}
} finally {
cursor.close();
}
}
}
// Unregister the widget IDs which are not present on the workspace. This could happen
// when a widget place holder is removed from workspace, before this method is called.
if (!idsToRemove.isEmpty()) {
final AppWidgetHost appWidgetHost =
new AppWidgetHost(context, Launcher.APPWIDGET_HOST_ID);
new AsyncTask<Void, Void, Void>() {
public Void doInBackground(Void ... args) {
for (Integer id : idsToRemove) {
appWidgetHost.deleteAppWidgetId(id);
Log.e(TAG, "Widget no longer present, appWidgetId=" + id);
}
return null;
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
}
LauncherAppState app = LauncherAppState.getInstanceNoCreate();
if (app != null) {
app.reloadWorkspace();
}
}
private static boolean setLayoutParams(Context context, AppWidgetManager appWidgetManager,
int appWidgetId, Bundle widgetIdOptions)
{
String preferenceKey = "isLargeLayout_"+appWidgetId;
boolean isLargeLayout;
SharedPreferences preferences = ApplicationPreferences.getSharedPreferences(context);
if (preferences.contains(preferenceKey))
// is already saved, use it
isLargeLayout = preferences.getBoolean(preferenceKey, true);
else
{
// is not saved, compute it
AppWidgetProviderInfo appWidgetProviderInfo = appWidgetManager.getAppWidgetInfo(appWidgetId);
int minHeight;
if (widgetIdOptions != null)
{
//int minWidth = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
//int maxWidth = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH);
minHeight = widgetIdOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT);
//int maxHeight = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT);
if ((minHeight == 0) && (appWidgetProviderInfo != null))
{
minHeight = appWidgetProviderInfo.minHeight;
}
}
else
{
if (appWidgetProviderInfo != null)
minHeight = appWidgetProviderInfo.minHeight;
else
minHeight = 0;
//if (minHeight == 0)
// return;
}
isLargeLayout = minHeight >= 110;
Editor editor = preferences.edit();
editor.putBoolean(preferenceKey, isLargeLayout);
editor.apply();
}
return isLargeLayout;
}
/**
* Updates the app widgets whose id has changed during the restore process.
*/
static void restoreAppWidgetIds(Context context, int[] oldWidgetIds, int[] newWidgetIds) {
final ContentResolver cr = context.getContentResolver();
final List<Integer> idsToRemove = new ArrayList<Integer>();
final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
for (int i = 0; i < oldWidgetIds.length; i++) {
Log.i(TAG, "Widget state restore id " + oldWidgetIds[i] + " => " + newWidgetIds[i]);
final AppWidgetProviderInfo provider = widgets.getAppWidgetInfo(newWidgetIds[i]);
final int state;
if (LauncherModel.isValidProvider(provider)) {
state = LauncherAppWidgetInfo.RESTORE_COMPLETED;
} else {
state = LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY;
}
ContentValues values = new ContentValues();
values.put(LauncherSettings.Favorites.APPWIDGET_ID, newWidgetIds[i]);
values.put(LauncherSettings.Favorites.RESTORED, state);
String[] widgetIdParams = new String[] { Integer.toString(oldWidgetIds[i]) };
int result = cr.update(Favorites.CONTENT_URI, values,
"appWidgetId=? and (restored & 1) = 1", widgetIdParams);
if (result == 0) {
Cursor cursor = cr.query(Favorites.CONTENT_URI,
new String[] {Favorites.APPWIDGET_ID},
"appWidgetId=?", widgetIdParams, null);
try {
if (!cursor.moveToFirst()) {
// The widget no long exists.
idsToRemove.add(newWidgetIds[i]);
}
} finally {
cursor.close();
}
}
}
// Unregister the widget IDs which are not present on the workspace. This could happen
// when a widget place holder is removed from workspace, before this method is called.
if (!idsToRemove.isEmpty()) {
final AppWidgetHost appWidgetHost =
new AppWidgetHost(context, Launcher.APPWIDGET_HOST_ID);
new AsyncTask<Void, Void, Void>() {
public Void doInBackground(Void ... args) {
for (Integer id : idsToRemove) {
appWidgetHost.deleteAppWidgetId(id);
Log.e(TAG, "Widget no longer present, appWidgetId=" + id);
}
return null;
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
}
}