Home | History | Annotate | Download | only in activity
      1 /*
      2  * Copyright (C) 2006 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.activity;
     18 
     19 import android.app.Service;
     20 import android.content.Intent;
     21 import android.os.Binder;
     22 import android.os.Bundle;
     23 import android.os.RemoteException;
     24 import android.os.IBinder;
     25 import android.os.Parcel;
     26 import android.util.Log;
     27 
     28 public class LocalService extends Service {
     29     private final IBinder mBinder = new Binder() {
     30 
     31         @Override
     32         protected boolean onTransact(int code, Parcel data, Parcel reply,
     33                 int flags) throws RemoteException {
     34             if (code == ServiceTest.SET_REPORTER_CODE) {
     35                 data.enforceInterface(ServiceTest.SERVICE_LOCAL);
     36                 mReportObject = data.readStrongBinder();
     37                 return true;
     38             } else {
     39                 return super.onTransact(code, data, reply, flags);
     40             }
     41         }
     42 
     43     };
     44 
     45     private IBinder mReportObject;
     46     private int mStartCount = 1;
     47 
     48     public LocalService() {
     49     }
     50 
     51     @Override
     52     public void onStart(Intent intent, int startId) {
     53         //Log.i("LocalService", "onStart: " + intent);
     54         if (intent.getExtras() != null) {
     55             mReportObject = intent.getExtras().getIBinder(ServiceTest.REPORT_OBJ_NAME);
     56             if (mReportObject != null) {
     57                 try {
     58                     Parcel data = Parcel.obtain();
     59                     data.writeInterfaceToken(ServiceTest.SERVICE_LOCAL);
     60                     data.writeInt(mStartCount);
     61                     mStartCount++;
     62                     mReportObject.transact(
     63                             ServiceTest.STARTED_CODE, data, null, 0);
     64                     data.recycle();
     65                 } catch (RemoteException e) {
     66                 }
     67             }
     68         }
     69     }
     70 
     71     @Override
     72     public void onDestroy() {
     73         Log.i("LocalService", "onDestroy: mReportObject=" + mReportObject);
     74         if (mReportObject != null) {
     75             try {
     76                 Parcel data = Parcel.obtain();
     77                 data.writeInterfaceToken(ServiceTest.SERVICE_LOCAL);
     78                 mReportObject.transact(
     79                         ServiceTest.DESTROYED_CODE, data, null, 0);
     80                 data.recycle();
     81             } catch (RemoteException e) {
     82             }
     83         }
     84     }
     85 
     86     @Override
     87     public IBinder onBind(Intent intent) {
     88         Log.i("LocalService", "onBind: " + intent);
     89         return mBinder;
     90     }
     91 
     92     @Override
     93     public boolean onUnbind(Intent intent) {
     94         Log.i("LocalService", "onUnbind: " + intent);
     95         if (mReportObject != null) {
     96             try {
     97                 Parcel data = Parcel.obtain();
     98                 data.writeInterfaceToken(ServiceTest.SERVICE_LOCAL);
     99                 mReportObject.transact(
    100                         ServiceTest.UNBIND_CODE, data, null, 0);
    101                 data.recycle();
    102             } catch (RemoteException e) {
    103             }
    104         }
    105         return true;
    106     }
    107 
    108     @Override
    109     public void onRebind(Intent intent) {
    110         Log.i("LocalService", "onUnbind: " + intent);
    111         if (mReportObject != null) {
    112             try {
    113                 Parcel data = Parcel.obtain();
    114                 data.writeInterfaceToken(ServiceTest.SERVICE_LOCAL);
    115                 mReportObject.transact(
    116                         ServiceTest.REBIND_CODE, data, null, 0);
    117                 data.recycle();
    118             } catch (RemoteException e) {
    119             }
    120         }
    121     }
    122 }
    123