Cleanup a bunch of warnings in app widgets code.

This commit is contained in:
Romain Guy
2009-07-01 17:20:08 -07:00
parent 392282f1e0
commit a54755962c
4 changed files with 55 additions and 61 deletions

View File

@@ -26,7 +26,6 @@ import android.widget.RemoteViews;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import com.android.internal.appwidget.IAppWidgetHost; import com.android.internal.appwidget.IAppWidgetHost;
import com.android.internal.appwidget.IAppWidgetService; import com.android.internal.appwidget.IAppWidgetService;
@@ -40,7 +39,7 @@ public class AppWidgetHost {
static final int HANDLE_UPDATE = 1; static final int HANDLE_UPDATE = 1;
static final int HANDLE_PROVIDER_CHANGED = 2; static final int HANDLE_PROVIDER_CHANGED = 2;
static Object sServiceLock = new Object(); final static Object sServiceLock = new Object();
static IAppWidgetService sService; static IAppWidgetService sService;
Context mContext; Context mContext;
@@ -85,7 +84,7 @@ public class AppWidgetHost {
int mHostId; int mHostId;
Callbacks mCallbacks = new Callbacks(); Callbacks mCallbacks = new Callbacks();
HashMap<Integer,AppWidgetHostView> mViews = new HashMap(); final HashMap<Integer,AppWidgetHostView> mViews = new HashMap<Integer, AppWidgetHostView>();
public AppWidgetHost(Context context, int hostId) { public AppWidgetHost(Context context, int hostId) {
mContext = context; mContext = context;
@@ -104,8 +103,8 @@ public class AppWidgetHost {
* becomes visible, i.e. from onStart() in your Activity. * becomes visible, i.e. from onStart() in your Activity.
*/ */
public void startListening() { public void startListening() {
int[] updatedIds = null; int[] updatedIds;
ArrayList<RemoteViews> updatedViews = new ArrayList(); ArrayList<RemoteViews> updatedViews = new ArrayList<RemoteViews>();
try { try {
if (mPackageName == null) { if (mPackageName == null) {
@@ -209,7 +208,7 @@ public class AppWidgetHost {
synchronized (mViews) { synchronized (mViews) {
mViews.put(appWidgetId, view); mViews.put(appWidgetId, view);
} }
RemoteViews views = null; RemoteViews views;
try { try {
views = sService.getAppWidgetViews(appWidgetId); views = sService.getAppWidgetViews(appWidgetId);
} catch (RemoteException e) { } catch (RemoteException e) {
@@ -231,6 +230,7 @@ public class AppWidgetHost {
/** /**
* Called when the AppWidget provider for a AppWidget has been upgraded to a new apk. * Called when the AppWidget provider for a AppWidget has been upgraded to a new apk.
*/ */
@SuppressWarnings({"UnusedDeclaration"})
protected void onProviderChanged(int appWidgetId, AppWidgetProviderInfo appWidget) { protected void onProviderChanged(int appWidgetId, AppWidgetProviderInfo appWidget) {
} }

View File

@@ -22,16 +22,12 @@ import android.graphics.Bitmap;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.Paint; import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock; import android.os.SystemClock;
import android.util.Config;
import android.util.Log; import android.util.Log;
import android.view.Gravity; import android.view.Gravity;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import android.widget.RemoteViews; import android.widget.RemoteViews;
import android.widget.TextView; import android.widget.TextView;
@@ -86,6 +82,7 @@ public class AppWidgetHostView extends FrameLayout {
* @param animationIn Resource ID of in animation to use * @param animationIn Resource ID of in animation to use
* @param animationOut Resource ID of out animation to use * @param animationOut Resource ID of out animation to use
*/ */
@SuppressWarnings({"UnusedDeclaration"})
public AppWidgetHostView(Context context, int animationIn, int animationOut) { public AppWidgetHostView(Context context, int animationIn, int animationOut) {
super(context); super(context);
mContext = context; mContext = context;

View File

@@ -20,10 +20,8 @@ import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException; import android.app.PendingIntent.CanceledException;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.PorterDuff; import android.graphics.PorterDuff;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.os.Parcel; import android.os.Parcel;
@@ -36,15 +34,12 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.LayoutInflater.Filter; import android.view.LayoutInflater.Filter;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import java.lang.Class; import java.lang.Class;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
@@ -70,12 +65,6 @@ public class RemoteViews implements Parcelable, Filter {
*/ */
private int mLayoutId; private int mLayoutId;
/**
* The Context object used to inflate the layout file. Also may
* be used by actions if they need access to the senders resources.
*/
private Context mContext;
/** /**
* An array of actions to perform on the view tree once it has been * An array of actions to perform on the view tree once it has been
* inflated * inflated
@@ -85,7 +74,7 @@ public class RemoteViews implements Parcelable, Filter {
/** /**
* This annotation indicates that a subclass of View is alllowed to be used * This annotation indicates that a subclass of View is alllowed to be used
* with the {@link android.widget.RemoteViews} mechanism. * with the {@link RemoteViews} mechanism.
*/ */
@Target({ ElementType.TYPE }) @Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@@ -116,7 +105,7 @@ public class RemoteViews implements Parcelable, Filter {
public int describeContents() { public int describeContents() {
return 0; return 0;
} }
}; }
/** /**
* Equivalent to calling * Equivalent to calling
@@ -232,6 +221,7 @@ public class RemoteViews implements Parcelable, Filter {
targetDrawable = imageView.getDrawable(); targetDrawable = imageView.getDrawable();
} }
if (targetDrawable != null) {
// Perform modifications only if values are set correctly // Perform modifications only if values are set correctly
if (alpha != -1) { if (alpha != -1) {
targetDrawable.setAlpha(alpha); targetDrawable.setAlpha(alpha);
@@ -243,6 +233,7 @@ public class RemoteViews implements Parcelable, Filter {
targetDrawable.setLevel(level); targetDrawable.setLevel(level);
} }
} }
}
int viewId; int viewId;
boolean targetBackground; boolean targetBackground;
@@ -289,6 +280,7 @@ public class RemoteViews implements Parcelable, Filter {
this.viewId = in.readInt(); this.viewId = in.readInt();
this.methodName = in.readString(); this.methodName = in.readString();
this.type = in.readInt(); this.type = in.readInt();
//noinspection ConstantIfStatement
if (false) { if (false) {
Log.d("RemoteViews", "read viewId=0x" + Integer.toHexString(this.viewId) Log.d("RemoteViews", "read viewId=0x" + Integer.toHexString(this.viewId)
+ " methodName=" + this.methodName + " type=" + this.type); + " methodName=" + this.methodName + " type=" + this.type);
@@ -340,31 +332,32 @@ public class RemoteViews implements Parcelable, Filter {
out.writeInt(this.viewId); out.writeInt(this.viewId);
out.writeString(this.methodName); out.writeString(this.methodName);
out.writeInt(this.type); out.writeInt(this.type);
//noinspection ConstantIfStatement
if (false) { if (false) {
Log.d("RemoteViews", "write viewId=0x" + Integer.toHexString(this.viewId) Log.d("RemoteViews", "write viewId=0x" + Integer.toHexString(this.viewId)
+ " methodName=" + this.methodName + " type=" + this.type); + " methodName=" + this.methodName + " type=" + this.type);
} }
switch (this.type) { switch (this.type) {
case BOOLEAN: case BOOLEAN:
out.writeInt(((Boolean)this.value).booleanValue() ? 1 : 0); out.writeInt((Boolean) this.value ? 1 : 0);
break; break;
case BYTE: case BYTE:
out.writeByte(((Byte)this.value).byteValue()); out.writeByte((Byte) this.value);
break; break;
case SHORT: case SHORT:
out.writeInt(((Short)this.value).shortValue()); out.writeInt((Short) this.value);
break; break;
case INT: case INT:
out.writeInt(((Integer)this.value).intValue()); out.writeInt((Integer) this.value);
break; break;
case LONG: case LONG:
out.writeLong(((Long)this.value).longValue()); out.writeLong((Long) this.value);
break; break;
case FLOAT: case FLOAT:
out.writeFloat(((Float)this.value).floatValue()); out.writeFloat((Float) this.value);
break; break;
case DOUBLE: case DOUBLE:
out.writeDouble(((Double)this.value).doubleValue()); out.writeDouble((Double) this.value);
break; break;
case CHAR: case CHAR:
out.writeInt((int)((Character)this.value).charValue()); out.writeInt((int)((Character)this.value).charValue());
@@ -430,7 +423,7 @@ public class RemoteViews implements Parcelable, Filter {
} }
Class klass = view.getClass(); Class klass = view.getClass();
Method method = null; Method method;
try { try {
method = klass.getMethod(this.methodName, getParameterType()); method = klass.getMethod(this.methodName, getParameterType());
} }
@@ -446,6 +439,7 @@ public class RemoteViews implements Parcelable, Filter {
} }
try { try {
//noinspection ConstantIfStatement
if (false) { if (false) {
Log.d("RemoteViews", "view: " + klass.getName() + " calling method: " Log.d("RemoteViews", "view: " + klass.getName() + " calling method: "
+ this.methodName + "(" + param.getName() + ") with " + this.methodName + "(" + param.getName() + ") with "
@@ -816,13 +810,12 @@ public class RemoteViews implements Parcelable, Filter {
* @return The inflated view hierarchy * @return The inflated view hierarchy
*/ */
public View apply(Context context, ViewGroup parent) { public View apply(Context context, ViewGroup parent) {
View result = null; View result;
Context c = prepareContext(context); Context c = prepareContext(context);
Resources r = c.getResources(); LayoutInflater inflater = (LayoutInflater)
LayoutInflater inflater = (LayoutInflater) c c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater = inflater.cloneInContext(c); inflater = inflater.cloneInContext(c);
inflater.setFilter(this); inflater.setFilter(this);
@@ -858,7 +851,7 @@ public class RemoteViews implements Parcelable, Filter {
} }
private Context prepareContext(Context context) { private Context prepareContext(Context context) {
Context c = null; Context c;
String packageName = mPackage; String packageName = mPackage;
if (packageName != null) { if (packageName != null) {
@@ -872,8 +865,6 @@ public class RemoteViews implements Parcelable, Filter {
c = context; c = context;
} }
mContext = c;
return c; return c;
} }

View File

@@ -29,7 +29,6 @@ import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.content.pm.ResolveInfo; import android.content.pm.ResolveInfo;
import android.content.pm.PackageItemInfo;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.content.res.XmlResourceParser; import android.content.res.XmlResourceParser;
import android.net.Uri; import android.net.Uri;
@@ -57,7 +56,6 @@ import java.util.HashSet;
import com.android.internal.appwidget.IAppWidgetService; import com.android.internal.appwidget.IAppWidgetService;
import com.android.internal.appwidget.IAppWidgetHost; import com.android.internal.appwidget.IAppWidgetHost;
import com.android.internal.util.XmlUtils;
import com.android.internal.util.FastXmlSerializer; import com.android.internal.util.FastXmlSerializer;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
@@ -80,7 +78,7 @@ class AppWidgetService extends IAppWidgetService.Stub
static class Provider { static class Provider {
int uid; int uid;
AppWidgetProviderInfo info; AppWidgetProviderInfo info;
ArrayList<AppWidgetId> instances = new ArrayList(); ArrayList<AppWidgetId> instances = new ArrayList<AppWidgetId>();
PendingIntent broadcast; PendingIntent broadcast;
boolean zombie; // if we're in safe mode, don't prune this just because nobody references it boolean zombie; // if we're in safe mode, don't prune this just because nobody references it
@@ -91,7 +89,7 @@ class AppWidgetService extends IAppWidgetService.Stub
int uid; int uid;
int hostId; int hostId;
String packageName; String packageName;
ArrayList<AppWidgetId> instances = new ArrayList(); ArrayList<AppWidgetId> instances = new ArrayList<AppWidgetId>();
IAppWidgetHost callbacks; IAppWidgetHost callbacks;
boolean zombie; // if we're in safe mode, don't prune this just because nobody references it boolean zombie; // if we're in safe mode, don't prune this just because nobody references it
@@ -108,10 +106,10 @@ class AppWidgetService extends IAppWidgetService.Stub
Context mContext; Context mContext;
PackageManager mPackageManager; PackageManager mPackageManager;
AlarmManager mAlarmManager; AlarmManager mAlarmManager;
ArrayList<Provider> mInstalledProviders = new ArrayList(); ArrayList<Provider> mInstalledProviders = new ArrayList<Provider>();
int mNextAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID + 1; int mNextAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID + 1;
ArrayList<AppWidgetId> mAppWidgetIds = new ArrayList(); final ArrayList<AppWidgetId> mAppWidgetIds = new ArrayList<AppWidgetId>();
ArrayList<Host> mHosts = new ArrayList(); ArrayList<Host> mHosts = new ArrayList<Host>();
boolean mSafeMode; boolean mSafeMode;
AppWidgetService(Context context) { AppWidgetService(Context context) {
@@ -175,7 +173,7 @@ class AppWidgetService extends IAppWidgetService.Stub
for (int i=0; i<N; i++) { for (int i=0; i<N; i++) {
AppWidgetId id = mAppWidgetIds.get(i); AppWidgetId id = mAppWidgetIds.get(i);
pw.print(" ["); pw.print(i); pw.print("] id="); pw.print(" ["); pw.print(i); pw.print("] id=");
pw.println(id.appWidgetId);; pw.println(id.appWidgetId);
pw.print(" hostId="); pw.print(" hostId=");
pw.print(id.host.hostId); pw.print(' '); pw.print(id.host.hostId); pw.print(' ');
pw.print(id.host.packageName); pw.print('/'); pw.print(id.host.packageName); pw.print('/');
@@ -385,7 +383,7 @@ class AppWidgetService extends IAppWidgetService.Stub
public List<AppWidgetProviderInfo> getInstalledProviders() { public List<AppWidgetProviderInfo> getInstalledProviders() {
synchronized (mAppWidgetIds) { synchronized (mAppWidgetIds) {
final int N = mInstalledProviders.size(); final int N = mInstalledProviders.size();
ArrayList<AppWidgetProviderInfo> result = new ArrayList(N); ArrayList<AppWidgetProviderInfo> result = new ArrayList<AppWidgetProviderInfo>(N);
for (int i=0; i<N; i++) { for (int i=0; i<N; i++) {
Provider p = mInstalledProviders.get(i); Provider p = mInstalledProviders.get(i);
if (!p.zombie) { if (!p.zombie) {
@@ -620,7 +618,6 @@ class AppWidgetService extends IAppWidgetService.Stub
// rely on the fact that we've already set it and that // rely on the fact that we've already set it and that
// PendingIntent.getBroadcast will update the extras. // PendingIntent.getBroadcast will update the extras.
boolean alreadyRegistered = p.broadcast != null; boolean alreadyRegistered = p.broadcast != null;
int instancesSize = p.instances.size();
Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE); Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
intent.setComponent(p.info.provider); intent.setComponent(p.info.provider);
@@ -780,10 +777,12 @@ class AppWidgetService extends IAppWidgetService.Stub
if (real.exists()) { if (real.exists()) {
readStateFromFileLocked(real); readStateFromFileLocked(real);
if (temp.exists()) { if (temp.exists()) {
//noinspection ResultOfMethodCallIgnored
temp.delete(); temp.delete();
} }
} else if (temp.exists()) { } else if (temp.exists()) {
readStateFromFileLocked(temp); readStateFromFileLocked(temp);
//noinspection ResultOfMethodCallIgnored
temp.renameTo(real); temp.renameTo(real);
} }
} }
@@ -799,18 +798,23 @@ class AppWidgetService extends IAppWidgetService.Stub
// use the temporary one until it's fully written, create an empty file // use the temporary one until it's fully written, create an empty file
// for real, which will we'll shortly delete. // for real, which will we'll shortly delete.
try { try {
//noinspection ResultOfMethodCallIgnored
real.createNewFile(); real.createNewFile();
} catch (IOException e) { } catch (IOException e) {
// Ignore
} }
} }
if (temp.exists()) { if (temp.exists()) {
//noinspection ResultOfMethodCallIgnored
temp.delete(); temp.delete();
} }
writeStateToFileLocked(temp); writeStateToFileLocked(temp);
//noinspection ResultOfMethodCallIgnored
real.delete(); real.delete();
//noinspection ResultOfMethodCallIgnored
temp.renameTo(real); temp.renameTo(real);
} }
@@ -873,8 +877,10 @@ class AppWidgetService extends IAppWidgetService.Stub
stream.close(); stream.close();
} }
} catch (IOException ex) { } catch (IOException ex) {
// Ignore
} }
if (file.exists()) { if (file.exists()) {
//noinspection ResultOfMethodCallIgnored
file.delete(); file.delete();
} }
} }
@@ -892,7 +898,7 @@ class AppWidgetService extends IAppWidgetService.Stub
int type; int type;
int providerIndex = 0; int providerIndex = 0;
HashMap<Integer,Provider> loadedProviders = new HashMap(); HashMap<Integer,Provider> loadedProviders = new HashMap<Integer, Provider>();
do { do {
type = parser.next(); type = parser.next();
if (type == XmlPullParser.START_TAG) { if (type == XmlPullParser.START_TAG) {
@@ -993,6 +999,7 @@ class AppWidgetService extends IAppWidgetService.Stub
stream.close(); stream.close();
} }
} catch (IOException e) { } catch (IOException e) {
// Ignore
} }
if (success) { if (success) {
@@ -1088,7 +1095,7 @@ class AppWidgetService extends IAppWidgetService.Stub
// TODO: If there's a better way of matching an intent filter against the // TODO: If there's a better way of matching an intent filter against the
// packages for a given package, use that. // packages for a given package, use that.
void updateProvidersForPackageLocked(String pkgName) { void updateProvidersForPackageLocked(String pkgName) {
HashSet<String> keep = new HashSet(); HashSet<String> keep = new HashSet<String>();
Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE); Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
List<ResolveInfo> broadcastReceivers = mPackageManager.queryBroadcastReceivers(intent, List<ResolveInfo> broadcastReceivers = mPackageManager.queryBroadcastReceivers(intent,
PackageManager.GET_META_DATA); PackageManager.GET_META_DATA);
@@ -1110,7 +1117,6 @@ class AppWidgetService extends IAppWidgetService.Stub
if (parsed != null) { if (parsed != null) {
keep.add(ai.name); keep.add(ai.name);
// Use the new AppWidgetProviderInfo. // Use the new AppWidgetProviderInfo.
AppWidgetProviderInfo oldInfo = p.info;
p.info = parsed.info; p.info = parsed.info;
// If it's enabled // If it's enabled
final int M = p.instances.size(); final int M = p.instances.size();