[Android 15] 在GlobalActionsDialog 中新增项目
需求: 在Power菜单弹出的对话框中新增“Volume/Ship mode/Suspend”
1. 新增ship_mode和suspend图标资源
frameworks/base/core/res/res/drawable/ic_ship_mode.xml
/frameworks/base/core/res/res/drawable/ic_suspend.xml
<!--
Copyright (C) 2024 Your Company/ProjectLicensed 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 athttp://www.apache.org/licenses/LICENSE-2.0Unless 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.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"android:width="32dp"android:height="32dp"android:viewportWidth="32"android:viewportHeight="32"android:tint="?attr/colorControlNormal"><pathandroid:fillColor="#4285F4"android:pathData="M0,22 L32,22 L32,32 L0,32 Z"android:strokeWidth="0"/><pathandroid:fillColor="#5D4037"android:pathData="M10,16 L22,16 L24,20 L8,20 Z"android:strokeWidth="0"/><pathandroid:fillColor="#8D6E63"android:pathData="M12,14 L20,14 L20,16 L12,16 Z"android:strokeWidth="0"/><pathandroid:fillColor="#E53935"android:pathData="M16,8 L22,14 L16,14 Z"android:strokeWidth="0"/><pathandroid:fillColor="#3E2723"android:pathData="M16,14 L16,16"android:strokeWidth="2"android:strokeColor="#3E2723"/>
</vector>
<!--
Copyright (C) 2024 Your Company/ProjectLicensed 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 athttp://www.apache.org/licenses/LICENSE-2.0Unless 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.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"android:width="32dp"android:height="32dp"android:viewportWidth="32"android:viewportHeight="32"android:tint="?attr/colorControlNormal"><pathandroid:fillColor="#000000"android:pathData="M16,2A14,14 0 1,0 16,30 10.5,10.5 0 0,1 16,2Z"android:strokeWidth="0"/><pathandroid:fillColor="#000000"android:pathData="M10,8h6v18h-6z"android:strokeWidth="0"/><pathandroid:fillColor="#000000"android:pathData="M18,8h6v18h-6z"android:strokeWidth="0"/>
</vector>
2.frameworks/base/core/res/res/values/config.xml
<string-array translatable="false" name="config_globalActionsList"><item>emergency</item><item>lockdown</item><item>power</item><item>restart</item><item>logout</item><item>screenshot</item><item>bugreport</item><item>battery_swap</item><!-- add items start--><item>suspend</item><item>adjust_volume</item><item>ship_mode</item><!-- add items end-->
</string-array>
3.frameworks/base/core/res/res/values/strings.xml
<!-- Notification messages for battery swap not available --><string name="backup_battery_low">Low backup battery</string><string name="backup_battery_charged">Backup battery is charged</string><string name="backup_battery_unavailable">Level of backup battery not available</string><string name="backup_battery_safe">You can safely swap the main battery</string><string name="backup_battery_wait">Please do not swap the main battery now.</string><string name="backup_battery_risky">It is risky to swap the main battery now.</string><!-- add items start --><string name="global_action_suspend">Suspend</string><string name="global_action_volume">Adjust volume</string><string name="global_action_ship_mode">Ship Mode</string><!-- add items end -->
4.frameworks/base/core/res/res/values/symbols.xml
<java-symbol type="drawable" name="ic_audio_vol" /><!-- add items start --><java-symbol type="drawable" name="ic_suspend" /><java-symbol type="drawable" name="ic_ship_mode" /><!-- add items end --><java-symbol type="drawable" name="ic_audio_vol_mute" /><java-symbol type="string" name="global_action_battery_swap" /><!-- add items start --><java-symbol type="string" name="global_action_suspend" /><java-symbol type="string" name="global_action_volume" /><java-symbol type="string" name="global_action_ship_mode" /><!-- add items end --><java-symbol type="string" name="invalidPuk" />
5.frameworks/base/packages/SystemUI/res/values/strings.xml
<string name="accessibility_power">Power</string><!-- add items start --><string name="global_action_suspend">Suspend</string><string name="global_action_volume">Adjust volume</string><string name="global_action_ship_mode">Ship Mode</string><!-- add items end -->
6.frameworks/base/packages/SystemUI/src/com/android/systemui/globalactions/GlobalActionsDialogLite.java
import android.os.PowerManager;static final String GLOBAL_ACTION_KEY_BATTERY_SWAP = "battery_swap";
// add items start
static final String GLOBAL_ACTION_KEY_SUSPEND = "suspend";
static final String GLOBAL_ACTION_KEY_ADJUST_VOLUME = "adjust_volume";
static final String GLOBAL_ACTION_KEY_SHIP_MODE = "ship_mode";
// add items end@VisibleForTesting
protected void createActionItems() {} else if (GLOBAL_ACTION_KEY_BATTERY_SWAP.equals(actionKey)) {addIfShouldShowAction(tempActions, new BatterySwapAction());
// add items start
} else if (GLOBAL_ACTION_KEY_SUSPEND.equals(actionKey)) {addIfShouldShowAction(tempActions, SuspendAction());
} else if (GLOBAL_ACTION_KEY_ADJUST_VOLUME.equals(actionKey)) {addIfShouldShowAction(tempActions, VolumeAction());
} else if (GLOBAL_ACTION_KEY_SHIP_MODE.equals(actionKey)) {addIfShouldShowAction(tempActions, ShipModeAction());
// add items end
} else {Log.e(TAG, "Invalid global action key " + actionKey);
}
// Add here so we don't add more than one.
addedKeys.add(actionKey);}private Action SuspendAction() {return new SinglePressAction(R.drawable.ic_suspend,R.string.global_action_suspend) {@Overridepublic void onPress() {PowerManager powerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);if (powerManager != null) {powerManager.goToSleep(SystemClock.uptimeMillis());}}@Overridepublic boolean showDuringKeyguard() {return true;}@Overridepublic boolean showBeforeProvisioning() {return true;}};}private Action VolumeAction() {return new SinglePressAction(R.drawable.ic_audio_vol,R.string.global_action_volume) {@Overridepublic void onPress() {mAudioManager.adjustStreamVolume(AudioManager.STREAM_SYSTEM,AudioManager.ADJUST_SAME,AudioManager.FLAG_SHOW_UI);}@Overridepublic boolean showDuringKeyguard() {return true;}@Overridepublic boolean showBeforeProvisioning() {return true;}};}private Action ShipModeAction() {return new SinglePressAction(R.drawable.ic_ship_mode,R.string.global_action_ship_mode) {@Overridepublic void onPress() {}@Overridepublic boolean showDuringKeyguard() {return true;}@Overridepublic boolean showBeforeProvisioning() {return true;}};}