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 com.example.android.intentplayground;
18 
19 import android.app.Activity;
20 import android.app.ActivityManager;
21 import android.app.FragmentManager;
22 import android.app.FragmentTransaction;
23 import android.content.ComponentName;
24 import android.content.Intent;
25 import android.os.Bundle;
26 import android.util.Log;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.widget.CheckBox;
30 import android.widget.LinearLayout;
31 
32 import java.util.ArrayList;
33 import java.util.LinkedList;
34 import java.util.List;
35 
36 /**
37  * All of the other activities extend BaseActivity, the shared functionality is implemented here
38  */
39 public abstract class BaseActivity extends Activity {
40     public final static String LAUNCH_FORWARD = "com.example.android.launchForward";
41     public final static String BUILDER_FRAGMENT = "com.example.android.builderFragment";
42     protected ComponentName mActivityToLaunch;
43     protected List<ActivityManager.AppTask> mTasks;
44 
45     @Override
onCreate(Bundle savedInstanceState)46     protected void onCreate(Bundle savedInstanceState) {
47         super.onCreate(savedInstanceState);
48         setContentView(R.layout.activity_main);
49         if (BuildConfig.DEBUG) Log.d(this.getLocalClassName(), "onCreate()");
50         Intent intent = getIntent();
51         FragmentManager fragmentManager = getFragmentManager();
52         FragmentTransaction transaction = fragmentManager.beginTransaction();
53         transaction.add(R.id.fragment_container, new CurrentTaskFragment());
54         TreeFragment currentTaskFrag = new TreeFragment();
55         Bundle args = new Bundle();
56         args.putString(TreeFragment.FRAGMENT_TITLE,
57                 getString(R.string.current_task_hierarchy_title));
58         currentTaskFrag.setArguments(args);
59         transaction.add(R.id.fragment_container, currentTaskFrag);
60 
61         if (intent.hasExtra(TestBase.EXPECTED_HIERARCHY)) {
62             // That means this activity was launched as a test show the result fragment
63             TreeFragment expectedView = new TreeFragment();
64             Bundle expectedArgs = new Bundle();
65             expectedArgs.putParcelable(TreeFragment.TREE_NODE,
66                     intent.getParcelableExtra(TestBase.EXPECTED_HIERARCHY));
67             expectedArgs.putString(TreeFragment.FRAGMENT_TITLE,
68                     getString(R.string.expected_task_hierarchy_title));
69             expectedView.setArguments(expectedArgs);
70             transaction.add(R.id.fragment_container, expectedView);
71         }
72 
73         transaction.add(R.id.fragment_container, new IntentFragment());
74         transaction.add(R.id.fragment_container, new IntentBuilderFragment(), BUILDER_FRAGMENT);
75         transaction.commit();
76 
77         if (intent.hasExtra(LAUNCH_FORWARD)) {
78             ArrayList<Intent> intents = intent.getParcelableArrayListExtra(LAUNCH_FORWARD);
79             if (!intents.isEmpty()) {
80                 Intent nextIntent = intents.remove(0);
81                 nextIntent.putParcelableArrayListExtra(LAUNCH_FORWARD, intents);
82                 if (BuildConfig.DEBUG) {
83                     Log.d(this.getLocalClassName(),
84                             LAUNCH_FORWARD + " " + nextIntent.getComponent().toString());
85                 }
86                 startActivity(nextIntent);
87             }
88         }
89     }
90 
91     /**
92      * Launches activity with the selected options
93      */
launchActivity(View view)94     public void launchActivity(View view) {
95         Intent customIntent = new Intent();
96         LinearLayout flagBuilder = findViewById(R.id.build_intent_flags);
97         // Gather flags from flag builder checkbox list
98         childrenOfGroup(flagBuilder, CheckBox.class)
99                 .forEach(checkbox -> {
100                     int flagVal = FlagUtils.value(checkbox.getText().toString());
101                     if (checkbox.isChecked()) customIntent.addFlags(flagVal);
102                     else customIntent.removeFlags(flagVal);
103                 });
104         customIntent.setComponent(mActivityToLaunch);
105         startActivity(customIntent);
106     }
107 
108     /**
109      * Convenience method to retrieve children of a certain type from a {@link ViewGroup}
110      * @param group the ViewGroup to retrieve children from
111      */
childrenOfGroup(ViewGroup group, Class<T> viewType)112     protected static <T> List<T> childrenOfGroup(ViewGroup group, Class<T> viewType) {
113         List<T> list = new LinkedList<>();
114         for (int i = 0; i < group.getChildCount(); i++) {
115             View v = group.getChildAt(i);
116             if (viewType.isAssignableFrom(v.getClass())) list.add(viewType.cast(v));
117         }
118         return list;
119     }
120 
121     @Override
onNewIntent(Intent intent)122     protected void onNewIntent(Intent intent) {
123         super.onNewIntent(intent);
124         setIntent(intent);
125     }
126 }
127