1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.app.cts; 18 19 import android.app.Service; 20 import android.content.Intent; 21 import android.cts.util.IBinderParcelable; 22 import android.os.Binder; 23 import android.os.IBinder; 24 import android.os.Parcel; 25 import android.os.RemoteException; 26 27 public class LocalService extends Service { 28 public static final String SERVICE_LOCAL = 29 "android.app.cts.activity.SERVICE_LOCAL"; 30 public static final String SERVICE_LOCAL_GRANTED = 31 "android.app.cts.activity.SERVICE_LOCAL_GRANTED"; 32 public static final String SERVICE_LOCAL_DENIED = 33 "android.app.cts.activity.SERVICE_LOCAL_DENIED"; 34 35 public static final String REPORT_OBJ_NAME = "report"; 36 37 public static final int STARTED_CODE = 1; 38 public static final int DESTROYED_CODE = 2; 39 public static final int SET_REPORTER_CODE = 3; 40 public static final int UNBIND_CODE = 4; 41 public static final int REBIND_CODE = 5; 42 43 private IBinder mReportObject; 44 private int mStartCount = 1; 45 46 private final IBinder mBinder = new Binder() { 47 @Override 48 protected boolean onTransact(int code, Parcel data, Parcel reply, 49 int flags) throws RemoteException { 50 if (code == SET_REPORTER_CODE) { 51 data.enforceInterface(SERVICE_LOCAL); 52 mReportObject = data.readStrongBinder(); 53 return true; 54 } else { 55 return super.onTransact(code, data, reply, flags); 56 } 57 } 58 }; 59 60 LocalService()61 public LocalService() { 62 } 63 64 @Override onStart(Intent intent, int startId)65 public void onStart(Intent intent, int startId) { 66 if (intent.getExtras() != null) { 67 IBinderParcelable parcelable 68 = (IBinderParcelable) intent.getExtras().getParcelable(REPORT_OBJ_NAME); 69 mReportObject = parcelable.binder; 70 if (mReportObject != null) { 71 bindAction(STARTED_CODE); 72 } 73 } 74 } 75 76 @Override onDestroy()77 public void onDestroy() { 78 if (mReportObject != null) { 79 bindAction(DESTROYED_CODE); 80 } 81 } 82 83 @Override onBind(Intent intent)84 public IBinder onBind(Intent intent) { 85 return mBinder; 86 } 87 88 @Override onUnbind(Intent intent)89 public boolean onUnbind(Intent intent) { 90 if (mReportObject != null) { 91 bindAction(UNBIND_CODE); 92 } 93 return true; 94 } 95 96 @Override onRebind(Intent intent)97 public void onRebind(Intent intent) { 98 if (mReportObject != null) { 99 bindAction(REBIND_CODE); 100 } 101 } 102 bindAction(final int bindCode)103 private void bindAction(final int bindCode) { 104 try { 105 Parcel data = Parcel.obtain(); 106 data.writeInterfaceToken(SERVICE_LOCAL); 107 if (bindCode == STARTED_CODE) { 108 data.writeInt(mStartCount); 109 mStartCount++; 110 } 111 mReportObject.transact( 112 bindCode, data, null, 0); 113 data.recycle(); 114 } catch (RemoteException e) { 115 // fail 116 } 117 } 118 } 119