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