1 /* 2 * Copyright (C) 2021 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.applications.specialaccess.notificationaccess; 16 17 import static com.android.settings.applications.AppInfoBase.ARG_PACKAGE_NAME; 18 19 import android.app.Application; 20 import android.app.settings.SettingsEnums; 21 import android.content.ComponentName; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.os.Bundle; 25 import android.os.UserHandle; 26 import android.provider.Settings; 27 import android.view.Menu; 28 import android.view.MenuInflater; 29 import android.view.MenuItem; 30 import android.view.View; 31 32 import com.android.settings.R; 33 import com.android.settings.dashboard.DashboardFragment; 34 import com.android.settings.notification.NotificationBackend; 35 import com.android.settingslib.applications.ApplicationsState; 36 import com.android.settingslib.applications.ApplicationsState.AppFilter; 37 38 public class BridgedAppsSettings extends DashboardFragment { 39 40 private static final String TAG = "BridgedAppsSettings"; 41 42 private static final int MENU_SHOW_SYSTEM = Menu.FIRST + 42; 43 private static final String EXTRA_SHOW_SYSTEM = "show_system"; 44 45 private boolean mShowSystem; 46 private AppFilter mFilter; 47 48 private int mUserId; 49 private ComponentName mComponentName; 50 51 @Override onCreate(Bundle icicle)52 public void onCreate(Bundle icicle) { 53 super.onCreate(icicle); 54 mShowSystem = icicle != null && icicle.getBoolean(EXTRA_SHOW_SYSTEM); 55 56 use(BridgedAppsPreferenceController.class).setNm(new NotificationBackend()); 57 } 58 59 @Override onCreateOptionsMenu(Menu menu, MenuInflater inflater)60 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 61 menu.add(Menu.NONE, MENU_SHOW_SYSTEM, Menu.NONE, 62 mShowSystem ? R.string.menu_hide_system : R.string.menu_show_system); 63 super.onCreateOptionsMenu(menu, inflater); 64 } 65 66 @Override onOptionsItemSelected(MenuItem item)67 public boolean onOptionsItemSelected(MenuItem item) { 68 switch (item.getItemId()) { 69 case MENU_SHOW_SYSTEM: 70 mShowSystem = !mShowSystem; 71 item.setTitle(mShowSystem ? R.string.menu_hide_system : R.string.menu_show_system); 72 mFilter = mShowSystem ? ApplicationsState.FILTER_NOT_HIDE 73 : ApplicationsState.FILTER_DOWNLOADED_AND_LAUNCHER; 74 75 use(BridgedAppsPreferenceController.class).setFilter(mFilter).rebuild(); 76 77 break; 78 } 79 return super.onOptionsItemSelected(item); 80 } 81 82 @Override onSaveInstanceState(Bundle outState)83 public void onSaveInstanceState(Bundle outState) { 84 super.onSaveInstanceState(outState); 85 outState.putBoolean(EXTRA_SHOW_SYSTEM, mShowSystem); 86 } 87 88 @Override onViewCreated(View view, Bundle savedInstanceState)89 public void onViewCreated(View view, Bundle savedInstanceState) { 90 super.onViewCreated(view, savedInstanceState); 91 } 92 93 @Override onAttach(Context context)94 public void onAttach(Context context) { 95 super.onAttach(context); 96 mFilter = mShowSystem ? ApplicationsState.FILTER_ALL_ENABLED 97 : ApplicationsState.FILTER_DOWNLOADED_AND_LAUNCHER; 98 99 final Bundle args = getArguments(); 100 Intent intent = (args == null) ? 101 getIntent() : (Intent) args.getParcelable("intent"); 102 String cn = args.getString(Settings.EXTRA_NOTIFICATION_LISTENER_COMPONENT_NAME); 103 if (cn != null) { 104 mComponentName = ComponentName.unflattenFromString(cn); 105 } 106 if (intent != null && intent.hasExtra(Intent.EXTRA_USER_HANDLE)) { 107 mUserId = ((UserHandle) intent.getParcelableExtra( 108 Intent.EXTRA_USER_HANDLE)).getIdentifier(); 109 } else { 110 mUserId = UserHandle.myUserId(); 111 } 112 113 use(BridgedAppsPreferenceController.class) 114 .setAppState(ApplicationsState.getInstance( 115 (Application) context.getApplicationContext())) 116 .setCn(mComponentName) 117 .setUserId(mUserId) 118 .setSession(getSettingsLifecycle()) 119 .setFilter(mFilter) 120 .rebuild(); 121 } 122 123 @Override getLogTag()124 protected String getLogTag() { 125 return TAG; 126 } 127 128 @Override getMetricsCategory()129 public int getMetricsCategory() { 130 return SettingsEnums.NOTIFICATION_ACCESS_BRIDGED_APPS; 131 } 132 133 @Override getPreferenceScreenResId()134 protected int getPreferenceScreenResId() { 135 return R.xml.notification_access_bridged_apps_settings; 136 } 137 } 138