SystemUI中QuickSettings增加截图按钮(基于Android Q)

1:首先Systemui中的res/values/config.xml :
新增screenshot

    
    "quick_settings_tiles_default" translatable="false">
        wifi,bt,dnd,flashlight,screenshot,location,rotation,battery,cell,airplane,cast
    

    
    "quick_settings_min_num_tiles">6

    
    "quick_settings_tiles_stock" translatable="false">
        wifi,cell,battery,dnd,flashlight,screenshot,rotation,bt,airplane,location,hotspot,inversion,saver,dark,work,cast,night
    

2:增加一个ScreenshotTile.java(照猫画虎,参考其他的title)

package com.android.systemui.qs.tiles;

import android.app.ActivityManager;
import android.content.Intent;
import android.service.quicksettings.Tile;
import android.widget.Switch;

import com.android.systemui.R;
import com.android.systemui.plugins.qs.QSTile.BooleanState;
import com.android.systemui.qs.QSHost;
import com.android.systemui.qs.tileimpl.QSTileImpl;
import com.android.internal.util.ScreenshotHelper;
import android.os.RemoteException;
import android.os.Handler;
import android.util.Log;
import android.os.Message;
import javax.inject.Inject;

/** Quick settings tile: Control flashlight **/
public class ScreenshotTile extends QSTileImpl<BooleanState>{

    private final Icon mIcon = ResourceIcon.get(com.android.internal.R.drawable.ic_screenshot);
    private ScreenshotHelper mScreenshotHelper;
    private Handler mHandler;

    //这个Inject还是必须要有的,不然会导致编译不过,一开始就入坑了
    @Inject
    public ScreenshotTile(QSHost host) {
        super(host);
        mHandler = new Handler();
        mScreenshotHelper = new ScreenshotHelper(mContext);
    }

    @Override
    public BooleanState newTileState() {
        return new BooleanState();
    }

    @Override
    protected void handleClick() {
        mHost.collapsePanels();

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.e(TAG, "gww ScreenshotAction onPress will takeScreenshot");
                //我这里是参考的原生的,长按power键出现的截图的方法。
                mScreenshotHelper.takeScreenshot(1, true, true, mHandler);
                }
            }, 500);
    }

    @Override
    protected void handleUpdateState(BooleanState state, Object arg) {
        state.icon = mIcon;
        state.label = mContext.getString(R.string.quick_settings_screenshot_label);
        state.contentDescription = mContext.getString(R.string.quick_settings_screenshot_label);
    }

    @Override
    public int getMetricsCategory() {
        return 0;
    }

    @Override
    public Intent getLongClickIntent() {
        return null;
    }

    @Override
    protected void handleSetListening(boolean listening) {

    }

    @Override
    public CharSequence getTileLabel() {
    //quick_settings_screenshot_label 这种字符串String.xml中添加就可以了
        return mContext.getString(R.string.quick_settings_screenshot_label);
    }

}

3:最重要的是添加title的地方
src/com/android/systemui/qs/tileimpl/QSFactoryImpl.java —>createTileInternal()

    private QSTileImpl createTileInternal(String tileSpec) {
        // Stock tiles.
        switch (tileSpec) {
        .......
            case "flashlight":
                return mFlashlightTileProvider.get();
                /*新增screenshot*/
            case "screenshot":
                return mScreenshotTileProvider.get();
public class QSFactoryImpl implements QSFactory {

    private static final String TAG = "QSFactory";

    private final Provider<WifiTile> mWifiTileProvider;
    private final Provider<BluetoothTile> mBluetoothTileProvider;
    private final Provider<CellularTile> mCellularTileProvider;
    private final Provider<DndTile> mDndTileProvider;
    private final Provider<ColorInversionTile> mColorInversionTileProvider;
    private final Provider<AirplaneModeTile> mAirplaneModeTileProvider;
    private final Provider<WorkModeTile> mWorkModeTileProvider;
    private final Provider<RotationLockTile> mRotationLockTileProvider;
    private final Provider<FlashlightTile> mFlashlightTileProvider;
    //**新增ScreenshotTile**
    private final Provider<ScreenshotTile> mScreenshotTileProvider;
   @Inject
    public QSFactoryImpl(Provider<WifiTile> wifiTileProvider,
            Provider<BluetoothTile> bluetoothTileProvider,
            Provider<CellularTile> cellularTileProvider,
            Provider<DndTile> dndTileProvider,
            Provider<ColorInversionTile> colorInversionTileProvider,
            Provider<AirplaneModeTile> airplaneModeTileProvider,
            Provider<WorkModeTile> workModeTileProvider,
            Provider<RotationLockTile> rotationLockTileProvider,
            Provider<FlashlightTile> flashlightTileProvider,
            //**ScreenshotTile**
            Provider<ScreenshotTile> screenshotTileProvider,
            Provider<LocationTile> locationTileProvider,
            Provider<CastTile> castTileProvider,
            Provider<HotspotTile> hotspotTileProvider,
            Provider<UserTile> userTileProvider,
            Provider<BatterySaverTile> batterySaverTileProvider,
            Provider<DataSaverTile> dataSaverTileProvider,
            Provider<NightDisplayTile> nightDisplayTileProvider,
            Provider<NfcTile> nfcTileProvider,
            Provider<GarbageMonitor.MemoryTile> memoryTileProvider,
            Provider<UiModeNightTile> uiModeNightTileProvider) {
        mWifiTileProvider = wifiTileProvider;
        mBluetoothTileProvider = bluetoothTileProvider;
        mCellularTileProvider = cellularTileProvider;
        mDndTileProvider = dndTileProvider;
        mColorInversionTileProvider = colorInversionTileProvider;
        mAirplaneModeTileProvider = airplaneModeTileProvider;
        mWorkModeTileProvider = workModeTileProvider;
        mRotationLockTileProvider = rotationLockTileProvider;
        mFlashlightTileProvider = flashlightTileProvider;
        //**ScreenshotTile**
        mScreenshotTileProvider = screenshotTileProvider;
        mLocationTileProvider = locationTileProvider;

主要代码就是如此,其他的小问题,解决即可

你可能感兴趣的:(SystemUI相关)