1 /*
2  * Copyright (C) 2019 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.flashlight;
18 
19 import android.app.Activity;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.Bundle;
23 import android.provider.Settings;
24 import android.util.Log;
25 
26 import com.android.settings.R;
27 import com.android.settings.search.BaseSearchIndexProvider;
28 import com.android.settingslib.search.Indexable;
29 import com.android.settingslib.search.SearchIndexable;
30 import com.android.settingslib.search.SearchIndexableRaw;
31 
32 import java.util.ArrayList;
33 import java.util.List;
34 
35 /**
36  * Headless activity that toggles flashlight state when launched.
37  */
38 @SearchIndexable(forTarget = SearchIndexable.MOBILE)
39 public class FlashlightHandleActivity extends Activity implements Indexable {
40 
41     public static final String EXTRA_FALLBACK_TO_HOMEPAGE = "fallback_to_homepage";
42 
43     private static final String TAG = "FlashlightActivity";
44     private static final String DATA_KEY = "flashlight";
45 
46     @Override
onCreate(Bundle savedInstanceState)47     protected void onCreate(Bundle savedInstanceState) {
48         super.onCreate(savedInstanceState);
49         // Do nothing meaningful in this activity.
50         // The sole purpose of this activity is to provide a place to index flashlight
51         // into Settings search.
52 
53         // Caller's choice: fallback to homepage, or just exit?
54         if (getIntent().getBooleanExtra(EXTRA_FALLBACK_TO_HOMEPAGE, false)) {
55             startActivity(new Intent(Settings.ACTION_SETTINGS).setPackage(getPackageName()));
56         }
57         finish();
58     }
59 
60     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
61             new BaseSearchIndexProvider() {
62 
63                 @Override
64                 public List<SearchIndexableRaw> getRawDataToIndex(Context context,
65                         boolean enabled) {
66 
67                     final List<SearchIndexableRaw> result = new ArrayList<>();
68                     if (!context.getResources().getBoolean(
69                             R.bool.config_settingsintelligence_slice_supported)) {
70                         Log.d(TAG, "Search doesn't support Slice");
71                         return result;
72                     }
73 
74                     SearchIndexableRaw data = new SearchIndexableRaw(context);
75                     data.title = context.getString(R.string.power_flashlight);
76                     data.screenTitle = context.getString(R.string.power_flashlight);
77                     data.keywords = context.getString(R.string.keywords_flashlight);
78                     data.intentTargetPackage = context.getPackageName();
79                     data.intentTargetClass = FlashlightHandleActivity.class.getName();
80                     data.intentAction = Intent.ACTION_MAIN;
81                     data.key = DATA_KEY;
82                     result.add(data);
83 
84                     return result;
85                 }
86 
87                 @Override
88                 public List<String> getNonIndexableKeys(Context context) {
89                     List<String> keys = super.getNonIndexableKeys(context);
90                     if (!FlashlightSlice.isFlashlightAvailable(context)) {
91                         Log.i(TAG, "Flashlight is unavailable");
92                         keys.add(DATA_KEY);
93                     }
94                     return keys;
95                 }
96             };
97 }
98