Merge "Button for opening app from Safety settings page" into sc-dev

This commit is contained in:
Fan Zhang
2021-03-25 02:38:12 +00:00
committed by Android (Google) Code Review
3 changed files with 159 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2021 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:settings="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dip"
android:layout_marginBottom="20dip"
style="@style/ActionPrimaryButton"
settings:allowDividerBelow="true"
android:gravity="center" />
</LinearLayout>

View File

@@ -32,6 +32,13 @@
android:order="100"
android:fragment="com.android.settings.emergency.EmergencyGestureSettings"
settings:controller="com.android.settings.emergency.EmergencyGestureEntrypointPreferenceController" />
<com.android.settingslib.widget.LayoutPreference
android:key="more_settings"
android:layout="@layout/more_settings_button"
android:order="180"
android:selectable="false"
settings:allowDividerBelow="true"
settings:controller="com.android.settings.emergency.MoreSettingsPreferenceController" />
<com.android.settingslib.RestrictedPreference
android:key="app_and_notif_cell_broadcast_settings"
android:title="@string/cell_broadcast_settings"

View File

@@ -0,0 +1,117 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.emergency;
import static android.content.pm.PackageManager.MATCH_DISABLED_COMPONENTS;
import static android.content.pm.PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS;
import static android.content.pm.PackageManager.MATCH_SYSTEM_ONLY;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import androidx.annotation.VisibleForTesting;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.widget.LayoutPreference;
import java.util.List;
/**
* Preference controller for More settings button
*/
public class MoreSettingsPreferenceController extends BasePreferenceController implements
View.OnClickListener {
private static final String EXTRA_KEY_ATTRIBUTION = "attribution";
private static final String TAG = "MoreSettingsPrefCtrl";
@VisibleForTesting
Intent mIntent;
public MoreSettingsPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
final String packageName = mContext.getResources().getString(
R.string.config_emergency_package_name);
if (TextUtils.isEmpty(packageName)) {
return;
}
mIntent = new Intent(Intent.ACTION_MAIN)
.setPackage(packageName);
final List<ResolveInfo> info = mContext.getPackageManager()
.queryIntentActivities(mIntent, MATCH_SYSTEM_ONLY);
if (info != null && !info.isEmpty()) {
mIntent.setClassName(packageName, info.get(0).activityInfo.name);
} else {
mIntent = null;
}
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
final LayoutPreference pref = screen.findPreference(getPreferenceKey());
final Button button = pref.findViewById(R.id.button);
button.setText(getButtonText());
button.setOnClickListener(this);
}
@Override
public int getAvailabilityStatus() {
if (mIntent == null) {
return UNSUPPORTED_ON_DEVICE;
} else {
return AVAILABLE;
}
}
@Override
public void onClick(View v) {
final Intent intent = new Intent(mIntent)
.addCategory(Intent.CATEGORY_LAUNCHER)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Bundle bundle = new Bundle();
bundle.putString(EXTRA_KEY_ATTRIBUTION, mContext.getPackageName());
mContext.startActivity(intent, bundle);
}
private CharSequence getButtonText() {
final String packageName = mContext.getResources().getString(
R.string.config_emergency_package_name);
try {
final PackageManager pm = mContext.getPackageManager();
final ApplicationInfo appInfo = pm.getApplicationInfo(
packageName, MATCH_DISABLED_COMPONENTS
| MATCH_DISABLED_UNTIL_USED_COMPONENTS);
return mContext.getString(R.string.open_app_button, appInfo.loadLabel(pm));
} catch (Exception e) {
Log.d(TAG, "Failed to get open app button text, falling back.");
return "";
}
}
}