| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.widget; |
| 18 |
|
| 19 |
import android.app.Service; |
| 20 |
import android.content.Intent; |
| 21 |
import android.os.IBinder; |
| 22 |
|
| 23 |
import com.android.internal.widget.IRemoteViewsFactory; |
| 24 |
|
| 25 |
import java.util.HashMap; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public abstract class RemoteViewsService extends Service { |
| 33 |
|
| 34 |
private static final String LOG_TAG = "RemoteViewsService"; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
private static final HashMap<Intent.FilterComparison, RemoteViewsFactory> sRemoteViewFactories = |
| 42 |
new HashMap<Intent.FilterComparison, RemoteViewsFactory>(); |
| 43 |
private static final Object sLock = new Object(); |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public interface RemoteViewsFactory { |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public void onCreate(); |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public void onDataSetChanged(); |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public void onDestroy(); |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public int getCount(); |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public RemoteViews getViewAt(int position); |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public RemoteViews getLoadingView(); |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public int getViewTypeCount(); |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public long getItemId(int position); |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public boolean hasStableIds(); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
private static class RemoteViewsFactoryAdapter extends IRemoteViewsFactory.Stub { |
| 133 |
public RemoteViewsFactoryAdapter(RemoteViewsFactory factory, boolean isCreated) { |
| 134 |
mFactory = factory; |
| 135 |
mIsCreated = isCreated; |
| 136 |
} |
| 137 |
public synchronized boolean isCreated() { |
| 138 |
return mIsCreated; |
| 139 |
} |
| 140 |
public synchronized void onDataSetChanged() { |
| 141 |
try { |
| 142 |
mFactory.onDataSetChanged(); |
| 143 |
} catch (Exception ex) { |
| 144 |
Thread t = Thread.currentThread(); |
| 145 |
Thread.getDefaultUncaughtExceptionHandler().uncaughtException(t, ex); |
| 146 |
} |
| 147 |
} |
| 148 |
public synchronized void onDataSetChangedAsync() { |
| 149 |
onDataSetChanged(); |
| 150 |
} |
| 151 |
public synchronized int getCount() { |
| 152 |
int count = 0; |
| 153 |
try { |
| 154 |
count = mFactory.getCount(); |
| 155 |
} catch (Exception ex) { |
| 156 |
Thread t = Thread.currentThread(); |
| 157 |
Thread.getDefaultUncaughtExceptionHandler().uncaughtException(t, ex); |
| 158 |
} |
| 159 |
return count; |
| 160 |
} |
| 161 |
public synchronized RemoteViews getViewAt(int position) { |
| 162 |
RemoteViews rv = null; |
| 163 |
try { |
| 164 |
rv = mFactory.getViewAt(position); |
| 165 |
if (rv != null) { |
| 166 |
rv.addFlags(RemoteViews.FLAG_WIDGET_IS_COLLECTION_CHILD); |
| 167 |
} |
| 168 |
} catch (Exception ex) { |
| 169 |
Thread t = Thread.currentThread(); |
| 170 |
Thread.getDefaultUncaughtExceptionHandler().uncaughtException(t, ex); |
| 171 |
} |
| 172 |
return rv; |
| 173 |
} |
| 174 |
public synchronized RemoteViews getLoadingView() { |
| 175 |
RemoteViews rv = null; |
| 176 |
try { |
| 177 |
rv = mFactory.getLoadingView(); |
| 178 |
} catch (Exception ex) { |
| 179 |
Thread t = Thread.currentThread(); |
| 180 |
Thread.getDefaultUncaughtExceptionHandler().uncaughtException(t, ex); |
| 181 |
} |
| 182 |
return rv; |
| 183 |
} |
| 184 |
public synchronized int getViewTypeCount() { |
| 185 |
int count = 0; |
| 186 |
try { |
| 187 |
count = mFactory.getViewTypeCount(); |
| 188 |
} catch (Exception ex) { |
| 189 |
Thread t = Thread.currentThread(); |
| 190 |
Thread.getDefaultUncaughtExceptionHandler().uncaughtException(t, ex); |
| 191 |
} |
| 192 |
return count; |
| 193 |
} |
| 194 |
public synchronized long getItemId(int position) { |
| 195 |
long id = 0; |
| 196 |
try { |
| 197 |
id = mFactory.getItemId(position); |
| 198 |
} catch (Exception ex) { |
| 199 |
Thread t = Thread.currentThread(); |
| 200 |
Thread.getDefaultUncaughtExceptionHandler().uncaughtException(t, ex); |
| 201 |
} |
| 202 |
return id; |
| 203 |
} |
| 204 |
public synchronized boolean hasStableIds() { |
| 205 |
boolean hasStableIds = false; |
| 206 |
try { |
| 207 |
hasStableIds = mFactory.hasStableIds(); |
| 208 |
} catch (Exception ex) { |
| 209 |
Thread t = Thread.currentThread(); |
| 210 |
Thread.getDefaultUncaughtExceptionHandler().uncaughtException(t, ex); |
| 211 |
} |
| 212 |
return hasStableIds; |
| 213 |
} |
| 214 |
public void onDestroy(Intent intent) { |
| 215 |
synchronized (sLock) { |
| 216 |
Intent.FilterComparison fc = new Intent.FilterComparison(intent); |
| 217 |
if (RemoteViewsService.sRemoteViewFactories.containsKey(fc)) { |
| 218 |
RemoteViewsFactory factory = RemoteViewsService.sRemoteViewFactories.get(fc); |
| 219 |
try { |
| 220 |
factory.onDestroy(); |
| 221 |
} catch (Exception ex) { |
| 222 |
Thread t = Thread.currentThread(); |
| 223 |
Thread.getDefaultUncaughtExceptionHandler().uncaughtException(t, ex); |
| 224 |
} |
| 225 |
RemoteViewsService.sRemoteViewFactories.remove(fc); |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
private RemoteViewsFactory mFactory; |
| 231 |
private boolean mIsCreated; |
| 232 |
} |
| 233 |
|
| 234 |
@Override |
| 235 |
public IBinder onBind(Intent intent) { |
| 236 |
synchronized (sLock) { |
| 237 |
Intent.FilterComparison fc = new Intent.FilterComparison(intent); |
| 238 |
RemoteViewsFactory factory = null; |
| 239 |
boolean isCreated = false; |
| 240 |
if (!sRemoteViewFactories.containsKey(fc)) { |
| 241 |
factory = onGetViewFactory(intent); |
| 242 |
sRemoteViewFactories.put(fc, factory); |
| 243 |
factory.onCreate(); |
| 244 |
isCreated = false; |
| 245 |
} else { |
| 246 |
factory = sRemoteViewFactories.get(fc); |
| 247 |
isCreated = true; |
| 248 |
} |
| 249 |
return new RemoteViewsFactoryAdapter(factory, isCreated); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
public abstract RemoteViewsFactory onGetViewFactory(Intent intent); |
| 258 |
} |