Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2012 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.uidisolation.cts;
     18 
     19 import android.app.Activity;
     20 import android.app.Service;
     21 import android.content.ComponentName;
     22 import android.content.Context;
     23 import android.content.Intent;
     24 import android.content.ServiceConnection;
     25 import android.os.Handler;
     26 import android.os.IBinder;
     27 import android.os.Message;
     28 import android.os.Messenger;
     29 import android.os.RemoteException;
     30 import android.util.Log;
     31 
     32 public class ServiceRunnerActivity extends Activity {
     33     private static String TAG = ServiceRunnerActivity.class.getName();
     34 
     35     private Messenger mService;
     36     private boolean mIsBound;
     37 
     38     private Boolean mSuccess;
     39 
     40     public synchronized Boolean getSuccess() {
     41         return mSuccess;
     42     }
     43 
     44     // Handler of incoming messages from service.
     45     class IncomingHandler extends Handler {
     46         private ServiceRunnerActivity mActivity;
     47 
     48         IncomingHandler(ServiceRunnerActivity activity) {
     49             mActivity = activity;
     50         }
     51 
     52         @Override
     53         public void handleMessage(Message msg) {
     54             switch (msg.what) {
     55                 case PermissionTestService.MSG_NOTIFY_TEST_SUCCESS:
     56                     synchronized (mActivity) {
     57                         mActivity.mSuccess = Boolean.TRUE;
     58                         mActivity.notify();
     59                     }
     60                     doUnbindService();
     61                    break;
     62                 case PermissionTestService.MSG_NOTIFY_TEST_FAILURE:
     63                     synchronized (mActivity) {
     64                         mActivity.mSuccess = Boolean.FALSE;
     65                         mActivity.notify();
     66                     }
     67                     doUnbindService();
     68                     break;
     69                default:
     70                    super.handleMessage(msg);
     71                    return;
     72             }
     73         }
     74     }
     75 
     76     // Target we publish for clients to send messages to IncomingHandler.
     77     final Messenger mMessenger = new Messenger(new IncomingHandler(this));
     78 
     79     private ServiceConnection mConnection = new ServiceConnection() {
     80         @Override
     81         public void onServiceConnected(ComponentName className, IBinder service) {
     82             mService = new Messenger(service);
     83 
     84             // Send a message to the service to register.
     85             try {
     86                 Message msg = Message.obtain(null, PermissionTestService.MSG_START_TEST);
     87                 msg.replyTo = mMessenger;
     88                 mService.send(msg);
     89             } catch (RemoteException e) {
     90                 // In this case the service has crashed before we could even do anything.
     91                 Log.e(TAG, "Failed to send start message to service.");
     92             }
     93         }
     94 
     95         @Override
     96         public void onServiceDisconnected(ComponentName className) {
     97             // This is called when the connection with the service has been unexpectedly
     98             // disconnected -- that is, its process crashed.
     99             Log.e(TAG, "Service disconnected.");
    100             mService = null;
    101         }
    102     };
    103 
    104 
    105     void doBindService(boolean isolated) {
    106         bindService(new Intent(this, isolated
    107                 ? IsolatedPermissionTestService.class
    108                 : PermissionTestService.class),
    109                 mConnection, Context.BIND_AUTO_CREATE);
    110         mIsBound = true;
    111     }
    112 
    113     void doUnbindService() {
    114         // Detach our existing connection.
    115         unbindService(mConnection);
    116         mIsBound = false;
    117     }
    118 
    119     void startNonIsolatedService() {
    120         doBindService(false);
    121     }
    122 
    123     void startIsolatedService() {
    124         doBindService(true);
    125     }
    126 }
    127