Search in sources :

Example 41 with AppWidgetProviderInfo

use of android.appwidget.AppWidgetProviderInfo in project android_frameworks_base by DirtyUnicorns.

the class AppWidgetServiceImpl method readProfileStateFromFileLocked.

private int readProfileStateFromFileLocked(FileInputStream stream, int userId, List<LoadedWidgetState> outLoadedWidgets) {
    int version = -1;
    try {
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(stream, StandardCharsets.UTF_8.name());
        int legacyProviderIndex = -1;
        int legacyHostIndex = -1;
        int type;
        do {
            type = parser.next();
            if (type == XmlPullParser.START_TAG) {
                String tag = parser.getName();
                if ("gs".equals(tag)) {
                    String attributeValue = parser.getAttributeValue(null, "version");
                    try {
                        version = Integer.parseInt(attributeValue);
                    } catch (NumberFormatException e) {
                        version = 0;
                    }
                } else if ("p".equals(tag)) {
                    legacyProviderIndex++;
                    // TODO: do we need to check that this package has the same signature
                    // as before?
                    String pkg = parser.getAttributeValue(null, "pkg");
                    String cl = parser.getAttributeValue(null, "cl");
                    pkg = getCanonicalPackageName(pkg, cl, userId);
                    if (pkg == null) {
                        continue;
                    }
                    final int uid = getUidForPackage(pkg, userId);
                    if (uid < 0) {
                        continue;
                    }
                    ComponentName componentName = new ComponentName(pkg, cl);
                    ActivityInfo providerInfo = getProviderInfo(componentName, userId);
                    if (providerInfo == null) {
                        continue;
                    }
                    ProviderId providerId = new ProviderId(uid, componentName);
                    Provider provider = lookupProviderLocked(providerId);
                    if (provider == null && mSafeMode) {
                        // if we're in safe mode, make a temporary one
                        provider = new Provider();
                        provider.info = new AppWidgetProviderInfo();
                        provider.info.provider = providerId.componentName;
                        provider.info.providerInfo = providerInfo;
                        provider.zombie = true;
                        provider.id = providerId;
                        mProviders.add(provider);
                    }
                    String tagAttribute = parser.getAttributeValue(null, "tag");
                    final int providerTag = !TextUtils.isEmpty(tagAttribute) ? Integer.parseInt(tagAttribute, 16) : legacyProviderIndex;
                    provider.tag = providerTag;
                } else if ("h".equals(tag)) {
                    legacyHostIndex++;
                    Host host = new Host();
                    // TODO: do we need to check that this package has the same signature
                    // as before?
                    String pkg = parser.getAttributeValue(null, "pkg");
                    final int uid = getUidForPackage(pkg, userId);
                    if (uid < 0) {
                        host.zombie = true;
                    }
                    if (!host.zombie || mSafeMode) {
                        // In safe mode, we don't discard the hosts we don't recognize
                        // so that they're not pruned from our list. Otherwise, we do.
                        final int hostId = Integer.parseInt(parser.getAttributeValue(null, "id"), 16);
                        String tagAttribute = parser.getAttributeValue(null, "tag");
                        final int hostTag = !TextUtils.isEmpty(tagAttribute) ? Integer.parseInt(tagAttribute, 16) : legacyHostIndex;
                        host.tag = hostTag;
                        host.id = new HostId(uid, hostId, pkg);
                        mHosts.add(host);
                    }
                } else if ("b".equals(tag)) {
                    String packageName = parser.getAttributeValue(null, "packageName");
                    final int uid = getUidForPackage(packageName, userId);
                    if (uid >= 0) {
                        Pair<Integer, String> packageId = Pair.create(userId, packageName);
                        mPackagesWithBindWidgetPermission.add(packageId);
                    }
                } else if ("g".equals(tag)) {
                    Widget widget = new Widget();
                    widget.appWidgetId = Integer.parseInt(parser.getAttributeValue(null, "id"), 16);
                    setMinAppWidgetIdLocked(userId, widget.appWidgetId + 1);
                    // restored ID is allowed to be absent
                    String restoredIdString = parser.getAttributeValue(null, "rid");
                    widget.restoredId = (restoredIdString == null) ? 0 : Integer.parseInt(restoredIdString, 16);
                    Bundle options = new Bundle();
                    String minWidthString = parser.getAttributeValue(null, "min_width");
                    if (minWidthString != null) {
                        options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, Integer.parseInt(minWidthString, 16));
                    }
                    String minHeightString = parser.getAttributeValue(null, "min_height");
                    if (minHeightString != null) {
                        options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, Integer.parseInt(minHeightString, 16));
                    }
                    String maxWidthString = parser.getAttributeValue(null, "max_width");
                    if (maxWidthString != null) {
                        options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, Integer.parseInt(maxWidthString, 16));
                    }
                    String maxHeightString = parser.getAttributeValue(null, "max_height");
                    if (maxHeightString != null) {
                        options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, Integer.parseInt(maxHeightString, 16));
                    }
                    String categoryString = parser.getAttributeValue(null, "host_category");
                    if (categoryString != null) {
                        options.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY, Integer.parseInt(categoryString, 16));
                    }
                    widget.options = options;
                    final int hostTag = Integer.parseInt(parser.getAttributeValue(null, "h"), 16);
                    String providerString = parser.getAttributeValue(null, "p");
                    final int providerTag = (providerString != null) ? Integer.parseInt(parser.getAttributeValue(null, "p"), 16) : TAG_UNDEFINED;
                    // We can match widgets with hosts and providers only after hosts
                    // and providers for all users have been loaded since the widget
                    // host and provider can be in different user profiles.
                    LoadedWidgetState loadedWidgets = new LoadedWidgetState(widget, hostTag, providerTag);
                    outLoadedWidgets.add(loadedWidgets);
                }
            }
        } while (type != XmlPullParser.END_DOCUMENT);
    } catch (NullPointerException | NumberFormatException | XmlPullParserException | IOException | IndexOutOfBoundsException e) {
        Slog.w(TAG, "failed parsing " + e);
        return -1;
    }
    return version;
}
Also used : ActivityInfo(android.content.pm.ActivityInfo) Bundle(android.os.Bundle) XmlPullParser(org.xmlpull.v1.XmlPullParser) IAppWidgetHost(com.android.internal.appwidget.IAppWidgetHost) IOException(java.io.IOException) Point(android.graphics.Point) WidgetBackupProvider(com.android.server.WidgetBackupProvider) AppWidgetProviderInfo(android.appwidget.AppWidgetProviderInfo) ComponentName(android.content.ComponentName) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) Pair(android.util.Pair)

Example 42 with AppWidgetProviderInfo

use of android.appwidget.AppWidgetProviderInfo in project android_frameworks_base by DirtyUnicorns.

the class AppWidgetServiceImpl method parseProviderInfoXml.

@SuppressWarnings("deprecation")
private Provider parseProviderInfoXml(ProviderId providerId, ResolveInfo ri) {
    Provider provider = null;
    ActivityInfo activityInfo = ri.activityInfo;
    XmlResourceParser parser = null;
    try {
        parser = activityInfo.loadXmlMetaData(mContext.getPackageManager(), AppWidgetManager.META_DATA_APPWIDGET_PROVIDER);
        if (parser == null) {
            Slog.w(TAG, "No " + AppWidgetManager.META_DATA_APPWIDGET_PROVIDER + " meta-data for " + "AppWidget provider '" + providerId + '\'');
            return null;
        }
        AttributeSet attrs = Xml.asAttributeSet(parser);
        int type;
        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && type != XmlPullParser.START_TAG) {
        // drain whitespace, comments, etc.
        }
        String nodeName = parser.getName();
        if (!"appwidget-provider".equals(nodeName)) {
            Slog.w(TAG, "Meta-data does not start with appwidget-provider tag for" + " AppWidget provider " + providerId.componentName + " for user " + providerId.uid);
            return null;
        }
        provider = new Provider();
        provider.id = providerId;
        AppWidgetProviderInfo info = provider.info = new AppWidgetProviderInfo();
        info.provider = providerId.componentName;
        info.providerInfo = activityInfo;
        final Resources resources;
        final long identity = Binder.clearCallingIdentity();
        try {
            final PackageManager pm = mContext.getPackageManager();
            final int userId = UserHandle.getUserId(providerId.uid);
            final ApplicationInfo app = pm.getApplicationInfoAsUser(activityInfo.packageName, 0, userId);
            resources = pm.getResourcesForApplication(app);
        } finally {
            Binder.restoreCallingIdentity(identity);
        }
        TypedArray sa = resources.obtainAttributes(attrs, com.android.internal.R.styleable.AppWidgetProviderInfo);
        // These dimensions has to be resolved in the application's context.
        // We simply send back the raw complex data, which will be
        // converted to dp in {@link AppWidgetManager#getAppWidgetInfo}.
        TypedValue value = sa.peekValue(com.android.internal.R.styleable.AppWidgetProviderInfo_minWidth);
        info.minWidth = value != null ? value.data : 0;
        value = sa.peekValue(com.android.internal.R.styleable.AppWidgetProviderInfo_minHeight);
        info.minHeight = value != null ? value.data : 0;
        value = sa.peekValue(com.android.internal.R.styleable.AppWidgetProviderInfo_minResizeWidth);
        info.minResizeWidth = value != null ? value.data : info.minWidth;
        value = sa.peekValue(com.android.internal.R.styleable.AppWidgetProviderInfo_minResizeHeight);
        info.minResizeHeight = value != null ? value.data : info.minHeight;
        info.updatePeriodMillis = sa.getInt(com.android.internal.R.styleable.AppWidgetProviderInfo_updatePeriodMillis, 0);
        info.initialLayout = sa.getResourceId(com.android.internal.R.styleable.AppWidgetProviderInfo_initialLayout, 0);
        info.initialKeyguardLayout = sa.getResourceId(com.android.internal.R.styleable.AppWidgetProviderInfo_initialKeyguardLayout, 0);
        String className = sa.getString(com.android.internal.R.styleable.AppWidgetProviderInfo_configure);
        if (className != null) {
            info.configure = new ComponentName(providerId.componentName.getPackageName(), className);
        }
        info.label = activityInfo.loadLabel(mContext.getPackageManager()).toString();
        info.icon = ri.getIconResource();
        info.previewImage = sa.getResourceId(com.android.internal.R.styleable.AppWidgetProviderInfo_previewImage, 0);
        info.autoAdvanceViewId = sa.getResourceId(com.android.internal.R.styleable.AppWidgetProviderInfo_autoAdvanceViewId, -1);
        info.resizeMode = sa.getInt(com.android.internal.R.styleable.AppWidgetProviderInfo_resizeMode, AppWidgetProviderInfo.RESIZE_NONE);
        info.widgetCategory = sa.getInt(com.android.internal.R.styleable.AppWidgetProviderInfo_widgetCategory, AppWidgetProviderInfo.WIDGET_CATEGORY_HOME_SCREEN);
        sa.recycle();
    } catch (IOException | PackageManager.NameNotFoundException | XmlPullParserException e) {
        // Ok to catch Exception here, because anything going wrong because
        // of what a client process passes to us should not be fatal for the
        // system process.
        Slog.w(TAG, "XML parsing failed for AppWidget provider " + providerId.componentName + " for user " + providerId.uid, e);
        return null;
    } finally {
        if (parser != null) {
            parser.close();
        }
    }
    return provider;
}
Also used : ActivityInfo(android.content.pm.ActivityInfo) XmlResourceParser(android.content.res.XmlResourceParser) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) ApplicationInfo(android.content.pm.ApplicationInfo) IOException(java.io.IOException) Point(android.graphics.Point) WidgetBackupProvider(com.android.server.WidgetBackupProvider) PackageManager(android.content.pm.PackageManager) IPackageManager(android.content.pm.IPackageManager) AttributeSet(android.util.AttributeSet) TypedArray(android.content.res.TypedArray) AppWidgetProviderInfo(android.appwidget.AppWidgetProviderInfo) ComponentName(android.content.ComponentName) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) Resources(android.content.res.Resources) TypedValue(android.util.TypedValue)

Example 43 with AppWidgetProviderInfo

use of android.appwidget.AppWidgetProviderInfo in project android_frameworks_base by DirtyUnicorns.

the class AppWidgetServiceImpl method dumpProvider.

private static void dumpProvider(Provider provider, int index, PrintWriter pw) {
    AppWidgetProviderInfo info = provider.info;
    pw.print("  [");
    pw.print(index);
    pw.print("] provider ");
    pw.println(provider.id);
    pw.print("    min=(");
    pw.print(info.minWidth);
    pw.print("x");
    pw.print(info.minHeight);
    pw.print(")   minResize=(");
    pw.print(info.minResizeWidth);
    pw.print("x");
    pw.print(info.minResizeHeight);
    pw.print(") updatePeriodMillis=");
    pw.print(info.updatePeriodMillis);
    pw.print(" resizeMode=");
    pw.print(info.resizeMode);
    pw.print(" widgetCategory=");
    pw.print(info.widgetCategory);
    pw.print(" autoAdvanceViewId=");
    pw.print(info.autoAdvanceViewId);
    pw.print(" initialLayout=#");
    pw.print(Integer.toHexString(info.initialLayout));
    pw.print(" initialKeyguardLayout=#");
    pw.print(Integer.toHexString(info.initialKeyguardLayout));
    pw.print(" zombie=");
    pw.println(provider.zombie);
}
Also used : AppWidgetProviderInfo(android.appwidget.AppWidgetProviderInfo)

Example 44 with AppWidgetProviderInfo

use of android.appwidget.AppWidgetProviderInfo in project platform_packages_apps_launcher by android.

the class Launcher method completeAddAppWidget.

/**
     * Add a widget to the workspace.
     *
     * @param data The intent describing the appWidgetId.
     * @param cellInfo The position on screen where to create the widget.
     */
private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo, boolean insertAtFirst) {
    Bundle extras = data.getExtras();
    int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
    d(LOG_TAG, "dumping extras content=" + extras.toString());
    AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
    // Calculate the grid spans needed to fit this widget
    CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
    int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
    // Try finding open space on Launcher screen
    final int[] xy = mCellCoordinates;
    if (!findSlot(cellInfo, xy, spans[0], spans[1]))
        return;
    // Build Launcher-specific widget info and save to database
    LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
    launcherInfo.spanX = spans[0];
    launcherInfo.spanY = spans[1];
    LauncherModel.addItemToDatabase(this, launcherInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP, mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
    if (!mRestoring) {
        sModel.addDesktopAppWidget(launcherInfo);
        // Perform actual inflation because we're live
        launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
        launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
        launcherInfo.hostView.setTag(launcherInfo);
        mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1], launcherInfo.spanX, launcherInfo.spanY, insertAtFirst);
    } else if (sModel.isDesktopLoaded()) {
        sModel.addDesktopAppWidget(launcherInfo);
    }
}
Also used : Bundle(android.os.Bundle) AppWidgetProviderInfo(android.appwidget.AppWidgetProviderInfo)

Example 45 with AppWidgetProviderInfo

use of android.appwidget.AppWidgetProviderInfo in project platform_packages_apps_launcher by android.

the class Launcher method addAppWidget.

void addAppWidget(Intent data) {
    // TODO: catch bad widget exception when sent
    int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
    String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
    if (SEARCH_WIDGET.equals(customWidget)) {
        // We don't need this any more, since this isn't a real app widget.
        mAppWidgetHost.deleteAppWidgetId(appWidgetId);
        // add the search widget
        addSearch();
    } else {
        AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
        if (appWidget.configure != null) {
            // Launch over to configure widget, if needed
            Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
            intent.setComponent(appWidget.configure);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
            startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
        } else {
            // Otherwise just add it
            onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
        }
    }
}
Also used : AppWidgetProviderInfo(android.appwidget.AppWidgetProviderInfo) Intent(android.content.Intent)

Aggregations

AppWidgetProviderInfo (android.appwidget.AppWidgetProviderInfo)91 Point (android.graphics.Point)24 ComponentName (android.content.ComponentName)21 AppWidgetHostView (android.appwidget.AppWidgetHostView)18 Bundle (android.os.Bundle)18 WidgetBackupProvider (com.android.server.WidgetBackupProvider)15 ActivityInfo (android.content.pm.ActivityInfo)14 ResolveInfo (android.content.pm.ResolveInfo)14 ArrayList (java.util.ArrayList)13 IOException (java.io.IOException)12 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)12 Intent (android.content.Intent)10 PackageManager (android.content.pm.PackageManager)9 Resources (android.content.res.Resources)7 View (android.view.View)7 Context (android.content.Context)6 IPackageManager (android.content.pm.IPackageManager)6 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)6 TypedArray (android.content.res.TypedArray)6 XmlResourceParser (android.content.res.XmlResourceParser)6