diff options
author | Dan Sandler <dsandler@android.com> | 2015-05-13 10:51:52 -0400 |
---|---|---|
committer | Dan Sandler <dsandler@android.com> | 2015-05-13 13:15:46 -0400 |
commit | 877d696c382ecb8a97972450c8819536641a963c (patch) | |
tree | 3bd31b9dd305f9ad8836eb013d9edaa07d512f64 /graphics | |
parent | 1c86159142aa8b5d582cd53e1d16b874c99d3bc4 (diff) | |
download | frameworks_base-877d696c382ecb8a97972450c8819536641a963c.zip frameworks_base-877d696c382ecb8a97972450c8819536641a963c.tar.gz frameworks_base-877d696c382ecb8a97972450c8819536641a963c.tar.bz2 |
Icon API tuneups:
- Reorder parameters to loadDrawableAsync()
- New version of createWithResource that takes a package
name instead of a Resources
- Add loadDrawableAsUser() for INTERACT_ACROSS_USERS clients
like SystemUI
- Docs cleanups
Bug: 21089268
Bug: 21031774
Change-Id: I465d2b865e35e12094b564f994e59d55e522f65a
Diffstat (limited to 'graphics')
-rw-r--r-- | graphics/java/android/graphics/drawable/Icon.java | 85 | ||||
-rw-r--r-- | graphics/tests/graphicstests/src/android/graphics/drawable/IconTest.java | 8 |
2 files changed, 66 insertions, 27 deletions
diff --git a/graphics/java/android/graphics/drawable/Icon.java b/graphics/java/android/graphics/drawable/Icon.java index 47a1f77..b2c3ab7 100644 --- a/graphics/java/android/graphics/drawable/Icon.java +++ b/graphics/java/android/graphics/drawable/Icon.java @@ -29,9 +29,9 @@ import android.os.Handler; import android.os.Message; import android.os.Parcel; import android.os.Parcelable; +import android.os.UserHandle; import android.util.Log; -import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -177,14 +177,13 @@ public final class Icon implements Parcelable { * Invokes {@link #loadDrawable(Context)} on a background thread * and then runs <code>andThen</code> on the UI thread when finished. * - * @param context {@link android.content.Context Context} in which to load the drawable; see + * @param context {@link Context Context} in which to load the drawable; see * {@link #loadDrawable(Context)} - * @param handler {@link android.os.Handler} on which to run <code>andThen</code>. * @param listener a callback to run on the provided - * Handler once the drawable is available. + * @param handler {@link Handler} on which to run <code>andThen</code>. */ - public void loadDrawableAsync(Context context, Handler handler, - final OnDrawableLoadedListener listener) { + public void loadDrawableAsync(Context context, final OnDrawableLoadedListener listener, + Handler handler) { new LoadDrawableTask(context, handler, listener).runAsync(); } @@ -211,14 +210,21 @@ public final class Icon implements Parcelable { try { mObj1 = pm.getResourcesForApplication(getResPackage()); } catch (PackageManager.NameNotFoundException e) { - Log.e(TAG, - String.format("Unable to find package '%s'", getResPackage()), + Log.e(TAG, String.format("Unable to find pkg=%s", + getResPackage()), e); break; } } } - return getResources().getDrawable(getResId(), context.getTheme()); + try { + return getResources().getDrawable(getResId(), context.getTheme()); + } catch (RuntimeException e) { + Log.e(TAG, String.format("Unable to load resource 0x%08x from pkg=%s", + getResId(), + getResPackage()), + e); + } case TYPE_DATA: return new BitmapDrawable(context.getResources(), BitmapFactory.decodeByteArray(getDataBytes(), getDataOffset(), getDataLength()) @@ -250,25 +256,62 @@ public final class Icon implements Parcelable { return null; } + /** + * Load the requested resources under the given userId, if the system allows it, + * before actually loading the drawable. + * + * @hide + */ + public Drawable loadDrawableAsUser(Context context, int userId) { + if (mType == TYPE_RESOURCE) { + if (getResources() == null + && getResPackage() != null + && !(getResPackage().equals("android"))) { + final PackageManager pm = context.getPackageManager(); + try { + mObj1 = pm.getResourcesForApplicationAsUser(getResPackage(), userId); + } catch (PackageManager.NameNotFoundException e) { + Log.e(TAG, String.format("Unable to find pkg=%s user=%d", + getResPackage(), + userId), + e); + } + } + } + return loadDrawable(context); + } + private Icon(int mType) { this.mType = mType; } /** - * Create a Icon pointing to a drawable resource. + * Create an Icon pointing to a drawable resource. * @param res Resources for a package containing the resource in question - * @param resid ID of the drawable resource + * @param resId ID of the drawable resource */ - public static Icon createWithResource(Resources res, @DrawableRes int resid) { + public static Icon createWithResource(Resources res, @DrawableRes int resId) { final Icon rep = new Icon(TYPE_RESOURCE); rep.mObj1 = res; - rep.mInt1 = resid; - rep.mString1 = res.getResourcePackageName(resid); + rep.mInt1 = resId; + rep.mString1 = res.getResourcePackageName(resId); + return rep; + } + + /** + * Create an Icon pointing to a drawable resource. + * @param resPackage Name of the package containing the resource in question + * @param resId ID of the drawable resource + */ + public static Icon createWithResource(String resPackage, @DrawableRes int resId) { + final Icon rep = new Icon(TYPE_RESOURCE); + rep.mInt1 = resId; + rep.mString1 = resPackage; return rep; } /** - * Create a Icon pointing to a bitmap in memory. + * Create an Icon pointing to a bitmap in memory. * @param bits A valid {@link android.graphics.Bitmap} object */ public static Icon createWithBitmap(Bitmap bits) { @@ -278,7 +321,7 @@ public final class Icon implements Parcelable { } /** - * Create a Icon pointing to a compressed bitmap stored in a byte array. + * Create an Icon pointing to a compressed bitmap stored in a byte array. * @param data Byte array storing compressed bitmap data of a type that * {@link android.graphics.BitmapFactory} * can decode (see {@link android.graphics.Bitmap.CompressFormat}). @@ -294,7 +337,7 @@ public final class Icon implements Parcelable { } /** - * Create a Icon pointing to a content specified by URI. + * Create an Icon pointing to an image file specified by URI. * * @param uri A uri referring to local content:// or file:// image data. */ @@ -305,7 +348,7 @@ public final class Icon implements Parcelable { } /** - * Create a Icon pointing to a content specified by URI. + * Create an Icon pointing to an image file specified by URI. * * @param uri A uri referring to local content:// or file:// image data. */ @@ -316,7 +359,7 @@ public final class Icon implements Parcelable { } /** - * Create a Icon pointing to + * Create an Icon pointing to an image file specified by path. * * @param path A path to a file that contains compressed bitmap data of * a type that {@link android.graphics.BitmapFactory} can decode. @@ -437,8 +480,8 @@ public final class Icon implements Parcelable { }; /** - * Implement this interface to receive notification when - * {@link #loadDrawableAsync(Context, Handler, OnDrawableLoadedListener) loadDrawableAsync} + * Implement this interface to receive a callback when + * {@link #loadDrawableAsync(Context, OnDrawableLoadedListener, Handler) loadDrawableAsync} * is finished and your Drawable is ready. */ public interface OnDrawableLoadedListener { diff --git a/graphics/tests/graphicstests/src/android/graphics/drawable/IconTest.java b/graphics/tests/graphicstests/src/android/graphics/drawable/IconTest.java index 2b9bf50..a7f8023 100644 --- a/graphics/tests/graphicstests/src/android/graphics/drawable/IconTest.java +++ b/graphics/tests/graphicstests/src/android/graphics/drawable/IconTest.java @@ -27,15 +27,11 @@ import android.util.Log; import java.io.ByteArrayOutputStream; import java.io.File; -import java.io.FileDescriptor; -import java.io.FileInputStream; import java.io.FileOutputStream; import java.lang.Override; import java.util.Arrays; import java.util.ArrayList; -import junit.framework.TestCase; - import com.android.frameworks.graphicstests.R; public class IconTest extends AndroidTestCase { @@ -173,7 +169,7 @@ public class IconTest extends AndroidTestCase { thd.start(); final Handler h = new Handler(thd.getLooper()); L(TAG, "asyncTest: dispatching load to thread: " + thd); - im1.loadDrawableAsync(mContext, h, new Icon.OnDrawableLoadedListener() { + im1.loadDrawableAsync(mContext, new Icon.OnDrawableLoadedListener() { @Override public void onDrawableLoaded(Drawable draw1) { L(TAG, "asyncTest: thread: loading drawable"); @@ -195,7 +191,7 @@ public class IconTest extends AndroidTestCase { fail("testAsync: file1 differs, check " + dir); } } - }); + }, h); L(TAG, "asyncTest: awaiting result"); Thread.sleep(500); // ;_; assertTrue("async-test.png does not exist!", new File(dir, "async-test.png").exists()); |