Search in sources :

Example 16 with Bitmap

use of android.graphics.Bitmap in project Launcher3 by chislon.

the class LauncherModel method getShortcutInfo.

/**
     * Make an ShortcutInfo object for a shortcut that isn't an application.
     */
private ShortcutInfo getShortcutInfo(Cursor c, Context context, int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex, int titleIndex) {
    Bitmap icon = null;
    final ShortcutInfo info = new ShortcutInfo();
    info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
    // TODO: If there's an explicit component and we can't install that, delete it.
    info.title = c.getString(titleIndex);
    int iconType = c.getInt(iconTypeIndex);
    switch(iconType) {
        case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
            String packageName = c.getString(iconPackageIndex);
            String resourceName = c.getString(iconResourceIndex);
            PackageManager packageManager = context.getPackageManager();
            info.customIcon = false;
            // the resource
            try {
                Resources resources = packageManager.getResourcesForApplication(packageName);
                if (resources != null) {
                    final int id = resources.getIdentifier(resourceName, null, null);
                    icon = Utilities.createIconBitmap(mIconCache.getFullResIcon(resources, id), context);
                }
            } catch (Exception e) {
            // drop this.  we have other places to look for icons
            }
            // the db
            if (icon == null) {
                icon = getIconFromCursor(c, iconIndex, context);
            }
            // the fallback icon
            if (icon == null) {
                icon = getFallbackIcon();
                info.usingFallbackIcon = true;
            }
            break;
        case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
            icon = getIconFromCursor(c, iconIndex, context);
            if (icon == null) {
                icon = getFallbackIcon();
                info.customIcon = false;
                info.usingFallbackIcon = true;
            } else {
                info.customIcon = true;
            }
            break;
        default:
            icon = getFallbackIcon();
            info.usingFallbackIcon = true;
            info.customIcon = false;
            break;
    }
    info.setIcon(icon);
    return info;
}
Also used : Bitmap(android.graphics.Bitmap) PackageManager(android.content.pm.PackageManager) Resources(android.content.res.Resources) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) URISyntaxException(java.net.URISyntaxException) RemoteException(android.os.RemoteException)

Example 17 with Bitmap

use of android.graphics.Bitmap in project Launcher3 by chislon.

the class LauncherModel method getShortcutInfo.

/**
     * Make an ShortcutInfo object for a shortcut that is an application.
     *
     * If c is not null, then it will be used to fill in missing data like the title and icon.
     */
public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context, Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
    ComponentName componentName = intent.getComponent();
    final ShortcutInfo info = new ShortcutInfo();
    if (componentName != null && !isValidPackageComponent(manager, componentName)) {
        Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
        return null;
    } else {
        try {
            PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
            info.initFlagsAndFirstInstallTime(pi);
        } catch (NameNotFoundException e) {
            Log.d(TAG, "getPackInfo failed for package " + componentName.getPackageName());
        }
    }
    // TODO: See if the PackageManager knows about this case.  If it doesn't
    // then return null & delete this.
    // the resource -- This may implicitly give us back the fallback icon,
    // but don't worry about that.  All we're doing with usingFallbackIcon is
    // to avoid saving lots of copies of that in the database, and most apps
    // have icons anyway.
    // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
    // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
    // via resolveActivity().
    Bitmap icon = null;
    ResolveInfo resolveInfo = null;
    ComponentName oldComponent = intent.getComponent();
    Intent newIntent = new Intent(intent.getAction(), null);
    newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    newIntent.setPackage(oldComponent.getPackageName());
    List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
    for (ResolveInfo i : infos) {
        ComponentName cn = new ComponentName(i.activityInfo.packageName, i.activityInfo.name);
        if (cn.equals(oldComponent)) {
            resolveInfo = i;
        }
    }
    if (resolveInfo == null) {
        resolveInfo = manager.resolveActivity(intent, 0);
    }
    if (resolveInfo != null) {
        icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
    }
    // the db
    if (icon == null) {
        if (c != null) {
            icon = getIconFromCursor(c, iconIndex, context);
        }
    }
    // the fallback icon
    if (icon == null) {
        icon = getFallbackIcon();
        info.usingFallbackIcon = true;
    }
    info.setIcon(icon);
    // from the resource
    if (resolveInfo != null) {
        ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
        if (labelCache != null && labelCache.containsKey(key)) {
            info.title = labelCache.get(key);
        } else {
            info.title = resolveInfo.activityInfo.loadLabel(manager);
            if (labelCache != null) {
                labelCache.put(key, info.title);
            }
        }
    }
    // from the db
    if (info.title == null) {
        if (c != null) {
            info.title = c.getString(titleIndex);
        }
    }
    // fall back to the class name of the activity
    if (info.title == null) {
        info.title = componentName.getClassName();
    }
    info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
    return info;
}
Also used : ResolveInfo(android.content.pm.ResolveInfo) Bitmap(android.graphics.Bitmap) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) PackageInfo(android.content.pm.PackageInfo)

Example 18 with Bitmap

use of android.graphics.Bitmap in project Launcher3 by chislon.

the class LauncherModel method updateSavedIcon.

void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
    boolean needSave = false;
    try {
        if (data != null) {
            Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
            Bitmap loaded = info.getIcon(mIconCache);
            needSave = !saved.sameAs(loaded);
        } else {
            needSave = true;
        }
    } catch (Exception e) {
        needSave = true;
    }
    if (needSave) {
        Log.d(TAG, "going to save icon bitmap for info=" + info);
        // This is slower than is ideal, but this only happens once
        // or when the app is updated with a new icon.
        updateItemInDatabase(context, info);
    }
}
Also used : Bitmap(android.graphics.Bitmap) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) URISyntaxException(java.net.URISyntaxException) RemoteException(android.os.RemoteException)

Example 19 with Bitmap

use of android.graphics.Bitmap in project Launcher3 by chislon.

the class HolographicViewHelper method createImageWithOverlay.

/**
     * Creates a new press state image which is the old image with a blue overlay.
     * Responsibility for the bitmap is transferred to the caller.
     */
private Bitmap createImageWithOverlay(ImageView v, Canvas canvas, int color) {
    final Drawable d = v.getDrawable();
    final Bitmap b = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    canvas.setBitmap(b);
    canvas.save();
    d.draw(canvas);
    canvas.restore();
    canvas.drawColor(color, PorterDuff.Mode.SRC_IN);
    canvas.setBitmap(null);
    return b;
}
Also used : Bitmap(android.graphics.Bitmap) StateListDrawable(android.graphics.drawable.StateListDrawable) Drawable(android.graphics.drawable.Drawable)

Example 20 with Bitmap

use of android.graphics.Bitmap in project Launcher3 by chislon.

the class LauncherBackupHelper method restoreIcon.

/**
     * Read an icon from the stream.
     *
     * <P>Keys arrive in any order, so shortcuts that use this icon may already exist.
     *
     * @param key identifier for the row
     * @param buffer the serialized proto from the stream, may be larger than dataSize
     * @param dataSize the size of the proto from the stream
     * @param keys keys to mark as clean in the notes for next backup
     */
private void restoreIcon(Key key, byte[] buffer, int dataSize, ArrayList<Key> keys) {
    Log.v(TAG, "unpacking icon " + key.id);
    if (DEBUG)
        Log.d(TAG, "read (" + buffer.length + "): " + Base64.encodeToString(buffer, 0, dataSize, Base64.NO_WRAP));
    try {
        Resource res = unpackIcon(buffer, 0, dataSize);
        if (DEBUG)
            Log.d(TAG, "unpacked " + res.dpi);
        if (DEBUG)
            Log.d(TAG, "read " + Base64.encodeToString(res.data, 0, res.data.length, Base64.NO_WRAP));
        Bitmap icon = BitmapFactory.decodeByteArray(res.data, 0, res.data.length);
        if (icon == null) {
            Log.w(TAG, "failed to unpack icon for " + key.name);
        }
    } catch (InvalidProtocolBufferNanoException e) {
        Log.w(TAG, "failed to decode proto", e);
    }
}
Also used : Bitmap(android.graphics.Bitmap) Resource(com.android.launcher3.backup.BackupProtos.Resource) InvalidProtocolBufferNanoException(com.google.protobuf.nano.InvalidProtocolBufferNanoException)

Aggregations

Bitmap (android.graphics.Bitmap)3662 Canvas (android.graphics.Canvas)875 Paint (android.graphics.Paint)697 BitmapDrawable (android.graphics.drawable.BitmapDrawable)447 IOException (java.io.IOException)384 Rect (android.graphics.Rect)338 Test (org.junit.Test)255 File (java.io.File)253 Matrix (android.graphics.Matrix)250 Drawable (android.graphics.drawable.Drawable)241 BitmapFactory (android.graphics.BitmapFactory)236 View (android.view.View)220 ImageView (android.widget.ImageView)205 FileOutputStream (java.io.FileOutputStream)182 Intent (android.content.Intent)177 InputStream (java.io.InputStream)165 FileNotFoundException (java.io.FileNotFoundException)150 RectF (android.graphics.RectF)148 Point (android.graphics.Point)146 Uri (android.net.Uri)117