| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.app.usage; |
| 18 |
|
| 19 |
import android.annotation.SystemApi; |
| 20 |
import android.app.Service; |
| 21 |
import android.app.usage.ICacheQuotaService; |
| 22 |
import android.content.Intent; |
| 23 |
import android.os.Bundle; |
| 24 |
import android.os.Handler; |
| 25 |
import android.os.IBinder; |
| 26 |
import android.os.Looper; |
| 27 |
import android.os.Message; |
| 28 |
import android.os.RemoteCallback; |
| 29 |
import android.util.Log; |
| 30 |
import android.util.Pair; |
| 31 |
|
| 32 |
import java.util.List; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
@SystemApi |
| 40 |
public abstract class CacheQuotaService extends Service { |
| 41 |
private static final String TAG = "CacheQuotaService"; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public static final String SERVICE_INTERFACE = "android.app.usage.CacheQuotaService"; |
| 48 |
|
| 49 |
|
| 50 |
public static final String REQUEST_LIST_KEY = "requests"; |
| 51 |
|
| 52 |
private CacheQuotaServiceWrapper mWrapper; |
| 53 |
private Handler mHandler; |
| 54 |
|
| 55 |
@Override |
| 56 |
public void onCreate() { |
| 57 |
super.onCreate(); |
| 58 |
mWrapper = new CacheQuotaServiceWrapper(); |
| 59 |
mHandler = new ServiceHandler(getMainLooper()); |
| 60 |
} |
| 61 |
|
| 62 |
@Override |
| 63 |
public IBinder onBind(Intent intent) { |
| 64 |
return mWrapper; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public abstract List<CacheQuotaHint> onComputeCacheQuotaHints( |
| 73 |
List<CacheQuotaHint> requests); |
| 74 |
|
| 75 |
private final class CacheQuotaServiceWrapper extends ICacheQuotaService.Stub { |
| 76 |
@Override |
| 77 |
public void computeCacheQuotaHints( |
| 78 |
RemoteCallback callback, List<CacheQuotaHint> requests) { |
| 79 |
final Pair<RemoteCallback, List<CacheQuotaHint>> pair = |
| 80 |
Pair.create(callback, requests); |
| 81 |
Message msg = mHandler.obtainMessage(ServiceHandler.MSG_SEND_LIST, pair); |
| 82 |
mHandler.sendMessage(msg); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
private final class ServiceHandler extends Handler { |
| 87 |
public static final int MSG_SEND_LIST = 1; |
| 88 |
|
| 89 |
public ServiceHandler(Looper looper) { |
| 90 |
super(looper, null, true); |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
public void handleMessage(Message msg) { |
| 95 |
final int action = msg.what; |
| 96 |
switch (action) { |
| 97 |
case MSG_SEND_LIST: |
| 98 |
final Pair<RemoteCallback, List<CacheQuotaHint>> pair = |
| 99 |
(Pair<RemoteCallback, List<CacheQuotaHint>>) msg.obj; |
| 100 |
List<CacheQuotaHint> processed = onComputeCacheQuotaHints(pair.second); |
| 101 |
final Bundle data = new Bundle(); |
| 102 |
data.putParcelableList(REQUEST_LIST_KEY, processed); |
| 103 |
|
| 104 |
final RemoteCallback callback = pair.first; |
| 105 |
callback.sendResult(data); |
| 106 |
break; |
| 107 |
default: |
| 108 |
Log.w(TAG, "Handling unknown message: " + action); |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
} |