| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.net; |
| 18 |
|
| 19 |
import android.Manifest.permission; |
| 20 |
import android.annotation.SystemApi; |
| 21 |
import android.content.Context; |
| 22 |
import android.os.Build; |
| 23 |
import android.os.Handler; |
| 24 |
import android.os.IBinder; |
| 25 |
import android.os.RemoteException; |
| 26 |
import android.util.Log; |
| 27 |
|
| 28 |
import com.android.internal.util.Preconditions; |
| 29 |
|
| 30 |
import java.util.concurrent.Executor; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
@SystemApi |
| 51 |
public abstract class NetworkRecommendationProvider { |
| 52 |
private static final String TAG = "NetworkRecProvider"; |
| 53 |
private static final boolean VERBOSE = Build.IS_DEBUGGABLE && Log.isLoggable(TAG, Log.VERBOSE); |
| 54 |
private final IBinder mService; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public NetworkRecommendationProvider(Context context, Executor executor) { |
| 62 |
Preconditions.checkNotNull(context); |
| 63 |
Preconditions.checkNotNull(executor); |
| 64 |
mService = new ServiceWrapper(context, executor); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public abstract void onRequestScores(NetworkKey[] networks); |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public final IBinder getBinder() { |
| 81 |
return mService; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
private final class ServiceWrapper extends INetworkRecommendationProvider.Stub { |
| 88 |
private final Context mContext; |
| 89 |
private final Executor mExecutor; |
| 90 |
private final Handler mHandler; |
| 91 |
|
| 92 |
ServiceWrapper(Context context, Executor executor) { |
| 93 |
mContext = context; |
| 94 |
mExecutor = executor; |
| 95 |
mHandler = null; |
| 96 |
} |
| 97 |
|
| 98 |
@Override |
| 99 |
public void requestScores(final NetworkKey[] networks) throws RemoteException { |
| 100 |
enforceCallingPermission(); |
| 101 |
if (networks != null && networks.length > 0) { |
| 102 |
execute(new Runnable() { |
| 103 |
@Override |
| 104 |
public void run() { |
| 105 |
onRequestScores(networks); |
| 106 |
} |
| 107 |
}); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
private void execute(Runnable command) { |
| 112 |
if (mExecutor != null) { |
| 113 |
mExecutor.execute(command); |
| 114 |
} else { |
| 115 |
mHandler.post(command); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
private void enforceCallingPermission() { |
| 120 |
if (mContext != null) { |
| 121 |
mContext.enforceCallingOrSelfPermission(permission.REQUEST_NETWORK_SCORES, |
| 122 |
"Permission denied."); |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
} |