From b87bd16c8e3f88a9ba78ed00c9f150b38dc39f33 Mon Sep 17 00:00:00 2001 From: Mike Cleron Date: Fri, 30 Oct 2009 16:36:56 -0700 Subject: [PATCH] More capabilities for default home configuration Added support for adding appwidgets beyond clock and search to the intital configuration. Also pulled in a previous change in Launcher that allowed http shortcuts. New syntax looks like this in default_workspace.xml: --- res/values/attrs.xml | 5 + .../android/launcher2/LauncherProvider.java | 96 ++++++++++++++++++- 2 files changed, 96 insertions(+), 5 deletions(-) diff --git a/res/values/attrs.xml b/res/values/attrs.xml index fb8a4df874..a0085bc401 100644 --- a/res/values/attrs.xml +++ b/res/values/attrs.xml @@ -76,5 +76,10 @@ + + + + + diff --git a/src/com/android/launcher2/LauncherProvider.java b/src/com/android/launcher2/LauncherProvider.java index dff72a29fb..9226c22538 100644 --- a/src/com/android/launcher2/LauncherProvider.java +++ b/src/com/android/launcher2/LauncherProvider.java @@ -17,6 +17,7 @@ package com.android.launcher2; import android.appwidget.AppWidgetHost; +import android.appwidget.AppWidgetManager; import android.content.ContentProvider; import android.content.Context; import android.content.ContentValues; @@ -24,6 +25,7 @@ import android.content.Intent; import android.content.ComponentName; import android.content.ContentUris; import android.content.ContentResolver; +import android.content.res.Resources; import android.content.res.XmlResourceParser; import android.content.res.TypedArray; import android.content.pm.PackageManager; @@ -42,6 +44,7 @@ import android.os.*; import android.provider.Settings; import java.io.IOException; +import java.net.URISyntaxException; import java.util.ArrayList; import org.xmlpull.v1.XmlPullParserException; @@ -175,7 +178,9 @@ public class LauncherProvider extends ContentProvider { private static final String TAG_FAVORITE = "favorite"; private static final String TAG_CLOCK = "clock"; private static final String TAG_SEARCH = "search"; - + private static final String TAG_APPWIDGET = "appwidget"; + private static final String TAG_SHORTCUT = "shortcut"; + private final Context mContext; private final AppWidgetHost mAppWidgetHost; @@ -531,11 +536,15 @@ public class LauncherProvider extends ContentProvider { a.getString(R.styleable.Favorite_y)); if (TAG_FAVORITE.equals(name)) { - added = addShortcut(db, values, a, packageManager, intent); + added = addAppShortcut(db, values, a, packageManager, intent); } else if (TAG_SEARCH.equals(name)) { added = addSearchWidget(db, values); } else if (TAG_CLOCK.equals(name)) { added = addClockWidget(db, values); + } else if (TAG_APPWIDGET.equals(name)) { + added = addAppWidget(db, values, a); + } else if (TAG_SHORTCUT.equals(name)) { + added = addUriShortcut(db, values, a); } if (added) i++; @@ -551,7 +560,7 @@ public class LauncherProvider extends ContentProvider { return i; } - private boolean addShortcut(SQLiteDatabase db, ContentValues values, TypedArray a, + private boolean addAppShortcut(SQLiteDatabase db, ContentValues values, TypedArray a, PackageManager packageManager, Intent intent) { ActivityInfo info; @@ -597,7 +606,7 @@ public class LauncherProvider extends ContentProvider { "com.android.alarmclock.AnalogAppWidgetProvider")); boolean allocatedAppWidgets = false; - + // Try binding to an analog clock widget try { int appWidgetId = mAppWidgetHost.allocateAppWidgetId(); @@ -620,8 +629,85 @@ public class LauncherProvider extends ContentProvider { return allocatedAppWidgets; } - } + + private boolean addAppWidget(SQLiteDatabase db, ContentValues values, TypedArray a) { + final int[] bindSources = new int[] { + Favorites.ITEM_TYPE_APPWIDGET, + }; + String packageName = a.getString(R.styleable.Favorite_packageName); + String className = a.getString(R.styleable.Favorite_className); + + if (packageName == null || className == null) { + return false; + } + + ComponentName cn = new ComponentName(packageName, className); + + final ArrayList bindTargets = new ArrayList(); + bindTargets.add(cn); + + boolean allocatedAppWidgets = false; + final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext); + + try { + int appWidgetId = mAppWidgetHost.allocateAppWidgetId(); + + values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET); + values.put(Favorites.SPANX, a.getString(R.styleable.Favorite_spanX)); + values.put(Favorites.SPANY, a.getString(R.styleable.Favorite_spanY)); + values.put(Favorites.APPWIDGET_ID, appWidgetId); + db.insert(TABLE_FAVORITES, null, values); + + allocatedAppWidgets = true; + + appWidgetManager.bindAppWidgetId(appWidgetId, cn); + } catch (RuntimeException ex) { + Log.e(LOG_TAG, "Problem allocating appWidgetId", ex); + } + + return allocatedAppWidgets; + } + + private boolean addUriShortcut(SQLiteDatabase db, ContentValues values, + TypedArray a) { + Resources r = mContext.getResources(); + + final int iconResId = a.getResourceId(R.styleable.Favorite_icon, 0); + final int titleResId = a.getResourceId(R.styleable.Favorite_title, 0); + + Intent intent = null; + String uri = null; + try { + uri = a.getString(R.styleable.Favorite_uri); + intent = Intent.parseUri(uri, 0); + } catch (URISyntaxException e) { + Log.w(LauncherModel.TAG, "Shortcut has malformed uri: " + uri); + return false; // Oh well + } + + if (iconResId == 0 || titleResId == 0) { + Log.w(LauncherModel.TAG, + "Shortcut is missing title or icon resource ID"); + return false; + } + + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + values.put(Favorites.INTENT, intent.toUri(0)); + values.put(Favorites.TITLE, r.getString(titleResId)); + values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_SHORTCUT); + values.put(Favorites.SPANX, 1); + values.put(Favorites.SPANY, 1); + values.put(Favorites.ICON_TYPE, Favorites.ICON_TYPE_RESOURCE); + values.put(Favorites.ICON_PACKAGE, mContext.getPackageName()); + values.put(Favorites.ICON_RESOURCE, r.getResourceName(iconResId)); + + db.insert(TABLE_FAVORITES, null, values); + + return true; + } + } + /** * Build a query string that will match any row where the column matches * anything in the values list.