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.android.settings.search;
18 
19 import static com.android.settings.SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS;
20 import static com.android.settings.SettingsActivity.EXTRA_SHOW_FRAGMENT_TAB;
21 
22 import android.app.Activity;
23 import android.content.ComponentName;
24 import android.content.Intent;
25 import android.os.Bundle;
26 import android.provider.Settings;
27 import android.text.TextUtils;
28 import android.util.Log;
29 
30 import com.android.settings.SettingsActivity;
31 import com.android.settings.SettingsApplication;
32 import com.android.settings.SubSettings;
33 import com.android.settings.activityembedding.ActivityEmbeddingRulesController;
34 import com.android.settings.activityembedding.ActivityEmbeddingUtils;
35 import com.android.settings.homepage.SettingsHomepageActivity;
36 import com.android.settings.overlay.FeatureFactory;
37 
38 import java.net.URISyntaxException;
39 
40 /**
41  * A trampoline activity that launches setting result page.
42  */
43 public class SearchResultTrampoline extends Activity {
44 
45     private static final String TAG = "SearchResultTrampoline";
46 
47     @Override
onCreate(Bundle savedInstanceState)48     protected void onCreate(Bundle savedInstanceState) {
49         super.onCreate(savedInstanceState);
50 
51         final ComponentName callingActivity = getCallingActivity();
52         // First make sure caller has privilege to launch a search result page.
53         FeatureFactory.getFactory(this)
54                 .getSearchFeatureProvider()
55                 .verifyLaunchSearchResultPageCaller(this, callingActivity);
56         // Didn't crash, proceed and launch the result as a subsetting.
57         Intent intent = getIntent();
58         final String highlightMenuKey = intent.getStringExtra(
59                 Settings.EXTRA_SETTINGS_EMBEDDED_DEEP_LINK_HIGHLIGHT_MENU_KEY);
60 
61         final String fragment = intent.getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT);
62         if (!TextUtils.isEmpty(fragment)) {
63             // Hack to take EXTRA_FRAGMENT_ARG_KEY from intent and set into
64             // EXTRA_SHOW_FRAGMENT_ARGUMENTS. This is necessary because intent could be from
65             // external caller and args may not persisted.
66             final String settingKey = intent.getStringExtra(
67                     SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
68             final int tab = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TAB, 0);
69             final Bundle args = new Bundle();
70             args.putString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY, settingKey);
71             args.putInt(EXTRA_SHOW_FRAGMENT_TAB, tab);
72             intent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
73 
74             // Reroute request to SubSetting.
75             intent.setClass(this /* context */, SubSettings.class);
76         } else {
77             // Direct link case
78             final String intentUriString = intent.getStringExtra(
79                     Settings.EXTRA_SETTINGS_EMBEDDED_DEEP_LINK_INTENT_URI);
80             if (TextUtils.isEmpty(intentUriString)) {
81                 Log.e(TAG, "No EXTRA_SETTINGS_EMBEDDED_DEEP_LINK_INTENT_URI for deep link");
82                 finish();
83                 return;
84             }
85 
86             try {
87                 intent = Intent.parseUri(intentUriString, Intent.URI_INTENT_SCHEME);
88             } catch (URISyntaxException e) {
89                 Log.e(TAG, "Failed to parse deep link intent: " + e);
90                 finish();
91                 return;
92             }
93         }
94 
95         intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
96 
97         if (!ActivityEmbeddingUtils.isEmbeddingActivityEnabled(this)) {
98             startActivity(intent);
99         } else if (isSettingsIntelligence(callingActivity)) {
100             // Register SplitPairRule for SubSettings, set clearTop false to prevent unexpected back
101             // navigation behavior.
102             ActivityEmbeddingRulesController.registerSubSettingsPairRule(this,
103                     false /* clearTop */);
104 
105             intent.setFlags(intent.getFlags() & ~Intent.FLAG_ACTIVITY_NEW_TASK);
106             startActivity(intent);
107 
108             // Pass menu key to homepage
109             final SettingsHomepageActivity homeActivity =
110                     ((SettingsApplication) getApplicationContext()).getHomeActivity();
111             if (homeActivity != null) {
112                 homeActivity.getMainFragment().setHighlightMenuKey(highlightMenuKey,
113                         /* scrollNeeded= */ true);
114             }
115         } else {
116             // Two-pane case
117             startActivity(SettingsActivity.getTrampolineIntent(intent, highlightMenuKey)
118                     .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
119         }
120 
121         // Done.
122         finish();
123     }
124 
isSettingsIntelligence(ComponentName callingActivity)125     private boolean isSettingsIntelligence(ComponentName callingActivity) {
126         return callingActivity != null && TextUtils.equals(
127                 callingActivity.getPackageName(),
128                 FeatureFactory.getFactory(this).getSearchFeatureProvider()
129                         .getSettingsIntelligencePkgName(this));
130     }
131 }
132