1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.settings.slices;
16 
17 import android.app.Activity;
18 import android.content.Intent;
19 import android.net.Uri;
20 import android.os.Bundle;
21 import android.provider.Settings;
22 import android.text.TextUtils;
23 import android.util.EventLog;
24 import android.util.Log;
25 
26 import com.android.settings.bluetooth.BluetoothSliceBuilder;
27 import com.android.settings.notification.ZenModeSliceBuilder;
28 
29 public class SliceDeepLinkSpringBoard extends Activity {
30 
31     private static final String TAG = "DeeplinkSpringboard";
32     public static final String EXTRA_SLICE = "slice";
33 
34     @Override
onCreate(Bundle savedInstanceState)35     protected void onCreate(Bundle savedInstanceState) {
36         super.onCreate(savedInstanceState);
37         final Uri sliceUri = parse(getIntent().getData());
38         if (sliceUri == null) {
39             Log.e(TAG, "No data found");
40             finish();
41             return;
42         }
43         try {
44             // This shouldn't matter since the slice is shown instead of the device
45             // index caring about the launch uri.
46             Intent launchIntent;
47 
48             // TODO (b/80263568) Avoid duplicating this list of Slice Uris.
49             if (CustomSliceRegistry.isValidUri(sliceUri)) {
50                 final CustomSliceable sliceable =
51                         CustomSliceable.createInstance(getApplicationContext(),
52                                 CustomSliceRegistry.getSliceClassByUri(sliceUri));
53                 launchIntent = sliceable.getIntent();
54             } else if (CustomSliceRegistry.ZEN_MODE_SLICE_URI.equals(sliceUri)) {
55                 launchIntent = ZenModeSliceBuilder.getIntent(this /* context */);
56             } else if (CustomSliceRegistry.BLUETOOTH_URI.equals(sliceUri)) {
57                 launchIntent = BluetoothSliceBuilder.getIntent(this /* context */);
58             } else {
59                 final SlicesDatabaseAccessor slicesDatabaseAccessor =
60                         new SlicesDatabaseAccessor(this /* context */);
61                 // Sadly have to block here because we don't know where to go.
62                 final SliceData sliceData =
63                         slicesDatabaseAccessor.getSliceDataFromUri(sliceUri);
64                 launchIntent = SliceBuilderUtils.getContentIntent(this, sliceData);
65             }
66             startActivity(launchIntent);
67             finish();
68         } catch (Exception e) {
69             Log.w(TAG, "Couldn't launch Slice intent", e);
70             startActivity(new Intent(Settings.ACTION_SETTINGS));
71             finish();
72         }
73     }
74 
parse(Uri uri)75     private static Uri parse(Uri uri) {
76         final String sliceParameter = uri.getQueryParameter(EXTRA_SLICE);
77         if (TextUtils.isEmpty(sliceParameter)) {
78             EventLog.writeEvent(0x534e4554, "122836081", -1, "");
79             return null;
80         } else {
81             return Uri.parse(sliceParameter);
82         }
83     }
84 }
85