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.content.BroadcastReceiver;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.content.ReceiverCallNotAllowedException;
25 import android.content.ServiceConnection;
26 import android.os.IBinder;
27 import android.os.Parcel;
28 import android.os.RemoteException;
29 
30 public class LocalReceiver extends BroadcastReceiver {
LocalReceiver()31     public LocalReceiver() {
32     }
33 
onReceive(Context context, Intent intent)34     public void onReceive(Context context, Intent intent) {
35         String resultString = LaunchpadActivity.RECEIVER_LOCAL;
36         if (BroadcastTest.BROADCAST_FAIL_REGISTER.equals(intent.getAction())) {
37             resultString = "Successfully registered, but expected it to fail";
38             try {
39                 context.registerReceiver(this, new IntentFilter("foo.bar"),
40                         Context.RECEIVER_EXPORTED_UNAUDITED);
41                 context.unregisterReceiver(this);
42             } catch (ReceiverCallNotAllowedException e) {
43                 //resultString = "This is the correct behavior but not yet implemented";
44                 resultString = LaunchpadActivity.RECEIVER_LOCAL;
45             }
46         } else if (BroadcastTest.BROADCAST_FAIL_BIND.equals(intent.getAction())) {
47             resultString = "Successfully bound to service, but expected it to fail";
48             try {
49                 ServiceConnection sc = new ServiceConnection() {
50                     public void onServiceConnected(ComponentName name, IBinder service) {
51                     }
52 
53                     public void onServiceDisconnected(ComponentName name) {
54                     }
55                 };
56                 context.bindService(new Intent(context, ServiceTest.RemoteService.class), sc, 0);
57                 context.unbindService(sc);
58             } catch (ReceiverCallNotAllowedException e) {
59                 //resultString = "This is the correct behavior but not yet implemented";
60                 resultString = LaunchpadActivity.RECEIVER_LOCAL;
61             }
62         } else if (LaunchpadActivity.BROADCAST_REPEAT.equals(intent.getAction())) {
63             Intent newIntent = new Intent(intent);
64             newIntent.setAction(LaunchpadActivity.BROADCAST_LOCAL);
65             context.sendOrderedBroadcast(newIntent, null);
66         }
67         try {
68             IBinder caller = intent.getIBinderExtra("caller");
69             Parcel data = Parcel.obtain();
70             data.writeInterfaceToken(LaunchpadActivity.LAUNCH);
71             data.writeString(resultString);
72             caller.transact(LaunchpadActivity.GOT_RECEIVE_TRANSACTION, data, null, 0);
73             data.recycle();
74         } catch (RemoteException ex) {
75         }
76     }
77 }
78 
79