• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.os.cts;
18 
19 import android.app.Service;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.ServiceConnection;
24 import android.os.IBinder;
25 
26 /**
27  * Service used to verify an UnsafeIntentLaunch StrictMode violation is reported when an Intent
28  * is unparceled from the delivered Intent and used to start / bind to another Service.
29  */
30 public class IntentLaunchService extends Service {
31     private static final String INNER_INTENT_KEY = "inner-intent";
32 
33     private static final ServiceConnection SERVICE_CONNECTION = new ServiceConnection() {
34         @Override
35         public void onServiceConnected(ComponentName className, IBinder service) {}
36 
37         @Override
38         public void onServiceDisconnected(ComponentName arg0) {}
39     };
40 
41     /**
42      * Returns an instance of a ServiceConnection that can be used when binding to this Service.
43      */
getServiceConnection()44     public static ServiceConnection getServiceConnection() {
45         return SERVICE_CONNECTION;
46     }
47 
48     /**
49      * Returns an Intent containing a parceled inner Intent that can be used to start / bind to this
50      * Service and verify the StrictMode UnsafeIntentLaunch check is reported as expected.
51      */
getTestIntent(Context context)52     public static Intent getTestIntent(Context context) {
53         Intent intent = new Intent(context, IntentLaunchService.class);
54         Intent innerIntent = new Intent(context, LocalService.class);
55         intent.putExtra(INNER_INTENT_KEY, innerIntent);
56         return intent;
57     }
58 
59     @Override
onBind(Intent intent)60     public IBinder onBind(Intent intent) {
61         Intent innerIntent = intent.getParcelableExtra(INNER_INTENT_KEY);
62         if (innerIntent != null) {
63             bindService(innerIntent, SERVICE_CONNECTION, Context.BIND_AUTO_CREATE);
64         }
65         return null;
66     }
67 
68     @Override
onStartCommand(Intent intent, int flags, int startId)69     public int onStartCommand(Intent intent, int flags, int startId) {
70         Intent innerIntent = intent.getParcelableExtra(INNER_INTENT_KEY);
71         if (innerIntent != null) {
72             startService(innerIntent);
73         }
74         stopService(intent);
75         return START_NOT_STICKY;
76     }
77 }
78