| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.service.resolver; |
| 18 |
|
| 19 |
import android.annotation.SdkConstant; |
| 20 |
import android.annotation.SystemApi; |
| 21 |
import android.app.Service; |
| 22 |
import android.content.ComponentName; |
| 23 |
import android.content.Intent; |
| 24 |
import android.os.IBinder; |
| 25 |
import android.os.Handler; |
| 26 |
import android.os.HandlerThread; |
| 27 |
import android.os.RemoteException; |
| 28 |
import android.service.resolver.ResolverTarget; |
| 29 |
import android.util.Log; |
| 30 |
|
| 31 |
import java.util.List; |
| 32 |
import java.util.Map; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
@SystemApi |
| 54 |
public abstract class ResolverRankerService extends Service { |
| 55 |
|
| 56 |
private static final String TAG = "ResolverRankerService"; |
| 57 |
|
| 58 |
private static final boolean DEBUG = false; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
@SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION) |
| 65 |
public static final String SERVICE_INTERFACE = "android.service.resolver.ResolverRankerService"; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public static final String HOLD_PERMISSION = "android.permission.PROVIDE_RESOLVER_RANKER_SERVICE"; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public static final String BIND_PERMISSION = "android.permission.BIND_RESOLVER_RANKER_SERVICE"; |
| 79 |
|
| 80 |
private ResolverRankerServiceWrapper mWrapper = null; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public void onPredictSharingProbabilities(final List<ResolverTarget> targets) {} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public void onTrainRankingModel( |
| 104 |
final List<ResolverTarget> targets, final int selectedPosition) {} |
| 105 |
|
| 106 |
private static final String HANDLER_THREAD_NAME = "RESOLVER_RANKER_SERVICE"; |
| 107 |
private volatile Handler mHandler; |
| 108 |
private HandlerThread mHandlerThread; |
| 109 |
|
| 110 |
@Override |
| 111 |
public IBinder onBind(Intent intent) { |
| 112 |
if (DEBUG) Log.d(TAG, "onBind " + intent); |
| 113 |
if (!SERVICE_INTERFACE.equals(intent.getAction())) { |
| 114 |
if (DEBUG) Log.d(TAG, "bad intent action " + intent.getAction() + "; returning null"); |
| 115 |
return null; |
| 116 |
} |
| 117 |
if (mHandlerThread == null) { |
| 118 |
mHandlerThread = new HandlerThread(HANDLER_THREAD_NAME); |
| 119 |
mHandlerThread.start(); |
| 120 |
mHandler = new Handler(mHandlerThread.getLooper()); |
| 121 |
} |
| 122 |
if (mWrapper == null) { |
| 123 |
mWrapper = new ResolverRankerServiceWrapper(); |
| 124 |
} |
| 125 |
return mWrapper; |
| 126 |
} |
| 127 |
|
| 128 |
@Override |
| 129 |
public void onDestroy() { |
| 130 |
mHandler = null; |
| 131 |
if (mHandlerThread != null) { |
| 132 |
mHandlerThread.quitSafely(); |
| 133 |
} |
| 134 |
super.onDestroy(); |
| 135 |
} |
| 136 |
|
| 137 |
private static void sendResult(List<ResolverTarget> targets, IResolverRankerResult result) { |
| 138 |
try { |
| 139 |
result.sendResult(targets); |
| 140 |
} catch (Exception e) { |
| 141 |
Log.e(TAG, "failed to send results: " + e); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
private class ResolverRankerServiceWrapper extends IResolverRankerService.Stub { |
| 146 |
|
| 147 |
@Override |
| 148 |
public void predict(final List<ResolverTarget> targets, final IResolverRankerResult result) |
| 149 |
throws RemoteException { |
| 150 |
Runnable predictRunnable = new Runnable() { |
| 151 |
@Override |
| 152 |
public void run() { |
| 153 |
try { |
| 154 |
if (DEBUG) { |
| 155 |
Log.d(TAG, "predict calls onPredictSharingProbabilities."); |
| 156 |
} |
| 157 |
onPredictSharingProbabilities(targets); |
| 158 |
sendResult(targets, result); |
| 159 |
} catch (Exception e) { |
| 160 |
Log.e(TAG, "onPredictSharingProbabilities failed; send null results: " + e); |
| 161 |
sendResult(null, result); |
| 162 |
} |
| 163 |
} |
| 164 |
}; |
| 165 |
final Handler h = mHandler; |
| 166 |
if (h != null) { |
| 167 |
h.post(predictRunnable); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
@Override |
| 172 |
public void train(final List<ResolverTarget> targets, final int selectedPosition) |
| 173 |
throws RemoteException { |
| 174 |
Runnable trainRunnable = new Runnable() { |
| 175 |
@Override |
| 176 |
public void run() { |
| 177 |
try { |
| 178 |
if (DEBUG) { |
| 179 |
Log.d(TAG, "train calls onTranRankingModel"); |
| 180 |
} |
| 181 |
onTrainRankingModel(targets, selectedPosition); |
| 182 |
} catch (Exception e) { |
| 183 |
Log.e(TAG, "onTrainRankingModel failed; skip train: " + e); |
| 184 |
} |
| 185 |
} |
| 186 |
}; |
| 187 |
final Handler h = mHandler; |
| 188 |
if (h != null) { |
| 189 |
h.post(trainRunnable); |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
} |