1 /*
2  * Copyright (C) 2017 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.server.cts.tools;
18 
19 import static android.content.Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT;
20 import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
21 import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
22 import static android.content.Intent.FLAG_ACTIVITY_REORDER_TO_FRONT;
23 
24 import android.app.ActivityOptions;
25 import android.content.Intent;
26 import android.content.ComponentName;
27 import android.content.Context;
28 import android.net.Uri;
29 import android.os.Bundle;
30 import android.server.cts.TestActivity;
31 import android.util.Log;
32 
33 /** Utility class which contains common code for launching activities. */
34 public class ActivityLauncher {
35     private static final String TAG = ActivityLauncher.class.getSimpleName();
36 
launchActivityFromExtras(final Context context, Bundle extras)37     public static void launchActivityFromExtras(final Context context, Bundle extras) {
38         if (extras == null || !extras.getBoolean("launch_activity")) {
39             return;
40         }
41 
42         Log.i(TAG, "launchActivityFromExtras: extras=" + extras);
43 
44         final Intent newIntent = new Intent();
45         final String targetActivity = extras.getString("target_activity");
46         if (targetActivity != null) {
47             final String extraPackageName = extras.getString("package_name");
48             final String packageName = extraPackageName != null ? extraPackageName
49                     : context.getApplicationContext().getPackageName();
50             newIntent.setComponent(new ComponentName(packageName,
51                     packageName + "." + targetActivity));
52         } else {
53             newIntent.setClass(context, TestActivity.class);
54         }
55 
56         if (extras.getBoolean("launch_to_the_side")) {
57             newIntent.addFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_LAUNCH_ADJACENT);
58             if (extras.getBoolean("random_data")) {
59                 final Uri data = new Uri.Builder()
60                         .path(String.valueOf(System.currentTimeMillis()))
61                         .build();
62                 newIntent.setData(data);
63             }
64         }
65         if (extras.getBoolean("multiple_task")) {
66             newIntent.addFlags(FLAG_ACTIVITY_MULTIPLE_TASK);
67         }
68         if (extras.getBoolean("new_task")) {
69             newIntent.addFlags(FLAG_ACTIVITY_NEW_TASK);
70         }
71 
72         if (extras.getBoolean("reorder_to_front")) {
73             newIntent.addFlags(FLAG_ACTIVITY_REORDER_TO_FRONT);
74         }
75 
76         ActivityOptions options = null;
77         final int displayId = extras.getInt("display_id", -1);
78         if (displayId != -1) {
79             options = ActivityOptions.makeBasic();
80             options.setLaunchDisplayId(displayId);
81             newIntent.addFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_MULTIPLE_TASK);
82         }
83 
84         context.startActivity(newIntent, options != null ? options.toBundle() : null);
85     }
86 }
87