Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ddfb2111 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add a flashlight slice in settings."

parents d0a8fcb2 97915b15
Loading
Loading
Loading
Loading
+28 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
     Copyright (C) 2017 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.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="48dp"
    android:height="48dp"
    android:viewportWidth="24.0"
    android:viewportHeight="24.0">
    <path
        android:fillColor="#FFFFFFFF"
        android:pathData="M18,2H6v6l2,3v11h8V11l2,-3L18,2zM16,4l0,1H8V4H16zM14,10.4V20h-4v-9.61l-2,-3V7h8l0,0.39L14,10.4z"/>
    <path
        android:fillColor="#FFFFFFFF"
        android:pathData="M12,14m-1.5,0a1.5,1.5 0,1 1,3 0a1.5,1.5 0,1 1,-3 0"/>
</vector>
+156 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 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.flashlight;

import static android.app.slice.Slice.EXTRA_TOGGLE_STATE;
import static androidx.slice.builders.ListBuilder.ICON_IMAGE;

import android.annotation.ColorInt;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;
import android.net.Uri;
import android.provider.Settings;
import android.provider.Settings.Secure;
import android.provider.SettingsSlicesContract;
import android.util.Log;

import androidx.core.graphics.drawable.IconCompat;
import com.android.settings.R;
import com.android.settings.Utils;
import com.android.settings.slices.SettingsSliceProvider;
import com.android.settings.slices.SliceBroadcastReceiver;

import androidx.slice.Slice;
import androidx.slice.builders.ListBuilder;
import androidx.slice.builders.SliceAction;

import android.app.StatusBarManager;


/**
 * Utility class to build a Flashlight Slice, and handle all associated actions.
 */
public class FlashlightSliceBuilder {

    private static final String TAG = "FlashlightSliceBuilder";

    public static final String KEY_FLASHLIGHT = "flashlight";

    /**
     * Backing Uri for the Flashlight Slice.
     */
    public static final Uri FLASHLIGHT_URI = new Uri.Builder()
        .scheme(ContentResolver.SCHEME_CONTENT)
        .authority(SettingsSliceProvider.SLICE_AUTHORITY)
        .appendPath(SettingsSlicesContract.PATH_SETTING_ACTION)
        .appendPath(KEY_FLASHLIGHT)
        .build();

    /**
     * Action notifying a change on the Flashlight Slice.
     */
    public static final String ACTION_FLASHLIGHT_SLICE_CHANGED =
        "com.android.settings.flashlight.action.FLASHLIGHT_SLICE_CHANGED";

    /**
     * Action broadcasting a change on whether flashlight is on or off.
     */
    public static final String ACTION_FLASHLIGHT_CHANGED =
        "com.android.settings.flashlight.action.FLASHLIGHT_CHANGED";

    public static final IntentFilter INTENT_FILTER = new IntentFilter(ACTION_FLASHLIGHT_CHANGED);

    private FlashlightSliceBuilder() {}

    /**
     * Return a Flashlight Slice bound to {@link #FLASHLIGHT_URI}.
     */
    public static Slice getSlice(Context context) {
        if (!isFlashlightAvailable(context)) {
            return null;
        }
        final PendingIntent toggleAction = getBroadcastIntent(context);
        @ColorInt final int color = Utils.getColorAccentDefaultColor(context);
        final IconCompat icon =
                IconCompat.createWithResource(context, R.drawable.ic_signal_flashlight);
        return new ListBuilder(context, FLASHLIGHT_URI, ListBuilder.INFINITY)
            .setAccentColor(color)
            .addRow(b -> b
                .setTitle(context.getText(R.string.power_flashlight))
                .setTitleItem(icon, ICON_IMAGE)
                .setPrimaryAction(
                        new SliceAction(toggleAction, null, isFlashlightEnabled(context))))
            .build();
    }

    /**
     * Update the current flashlight status to the boolean value keyed by
     * {@link android.app.slice.Slice#EXTRA_TOGGLE_STATE} on {@param intent}.
     */
    public static void handleUriChange(Context context, Intent intent) {
        try {
            final String cameraId = getCameraId(context);
            if (cameraId != null) {
                final boolean state = intent.getBooleanExtra(
                        EXTRA_TOGGLE_STATE, isFlashlightEnabled(context));
                final CameraManager cameraManager = context.getSystemService(CameraManager.class);
                cameraManager.setTorchMode(cameraId, state);
            }
        } catch (CameraAccessException e) {
            Log.e(TAG, "Camera couldn't set torch mode.", e);
        }
        context.getContentResolver().notifyChange(FLASHLIGHT_URI, null);
    }

    private static String getCameraId(Context context) throws CameraAccessException {
        final CameraManager cameraManager = context.getSystemService(CameraManager.class);
        final String[] ids = cameraManager.getCameraIdList();
        for (String id : ids) {
            CameraCharacteristics c = cameraManager.getCameraCharacteristics(id);
            Boolean flashAvailable = c.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
            Integer lensFacing = c.get(CameraCharacteristics.LENS_FACING);
            if (flashAvailable != null && flashAvailable
                  && lensFacing != null && lensFacing == CameraCharacteristics.LENS_FACING_BACK) {
                return id;
            }
        }
        return null;
    }

    private static PendingIntent getBroadcastIntent(Context context) {
        final Intent intent = new Intent(ACTION_FLASHLIGHT_SLICE_CHANGED);
        intent.setClass(context, SliceBroadcastReceiver.class);
        return PendingIntent.getBroadcast(context, 0 /* requestCode */, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    }

    private static boolean isFlashlightAvailable(Context context) {
        return Settings.Secure.getInt(
            context.getContentResolver(), Secure.FLASHLIGHT_AVAILABLE, 0) == 1;
    }

    private static boolean isFlashlightEnabled(Context context) {
        return Settings.Secure.getInt(
            context.getContentResolver(), Secure.FLASHLIGHT_ENABLED, 0) == 1;
    }
}
+36 −27
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ import android.util.Pair;

import com.android.settings.bluetooth.BluetoothSliceBuilder;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.flashlight.FlashlightSliceBuilder;
import com.android.settings.location.LocationSliceBuilder;
import com.android.settings.notification.ZenModeSliceBuilder;
import com.android.settings.mobilenetwork.Enhanced4gLteSliceHelper;
@@ -160,6 +161,10 @@ public class SettingsSliceProvider extends SliceProvider {
        } else if (BluetoothSliceBuilder.BLUETOOTH_URI.equals(sliceUri)) {
            registerIntentToUri(BluetoothSliceBuilder.INTENT_FILTER, sliceUri);
            return;
        } else if (FlashlightSliceBuilder.FLASHLIGHT_URI.equals(sliceUri)) {
            registerIntentToUri(FlashlightSliceBuilder.INTENT_FILTER , sliceUri);
            mRegisteredUris.add(sliceUri);
            return;
        }

        // Start warming the slice, we expect someone will want it soon.
@@ -191,6 +196,7 @@ public class SettingsSliceProvider extends SliceProvider {
                Log.e(TAG, "Requested blocked slice with Uri: " + sliceUri);
                return null;
            }

            // If adding a new Slice, do not directly match Slice URIs.
            // Use {@link SlicesDatabaseAccessor}.
            if (WifiCallingSliceHelper.WIFI_CALLING_URI.equals(sliceUri)) {
@@ -216,6 +222,8 @@ public class SettingsSliceProvider extends SliceProvider {
                        .getSlicesFeatureProvider()
                        .getNewWifiCallingSliceHelper(getContext())
                        .createWifiCallingPreferenceSlice(sliceUri);
            } else if (FlashlightSliceBuilder.FLASHLIGHT_URI.equals(sliceUri)) {
                return FlashlightSliceBuilder.getSlice(getContext());
            }

            SliceData cachedSliceData = mSliceWeakDataCache.get(sliceUri);
@@ -381,7 +389,8 @@ public class SettingsSliceProvider extends SliceProvider {

    private List<Uri> getSpecialCaseOemUris() {
        return Arrays.asList(
                ZenModeSliceBuilder.ZEN_MODE_URI
                ZenModeSliceBuilder.ZEN_MODE_URI,
                FlashlightSliceBuilder.FLASHLIGHT_URI
        );
    }

+5 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.settings.slices;

import static com.android.settings.bluetooth.BluetoothSliceBuilder.ACTION_BLUETOOTH_SLICE_CHANGED;
import static com.android.settings.flashlight.FlashlightSliceBuilder.ACTION_FLASHLIGHT_SLICE_CHANGED;
import static com.android.settings.notification.ZenModeSliceBuilder.ACTION_ZEN_MODE_SLICE_CHANGED;
import static com.android.settings.slices.SettingsSliceProvider.ACTION_SLIDER_CHANGED;
import static com.android.settings.slices.SettingsSliceProvider.ACTION_TOGGLE_CHANGED;
@@ -45,6 +46,7 @@ import com.android.settings.bluetooth.BluetoothSliceBuilder;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.SliderPreferenceController;
import com.android.settings.core.TogglePreferenceController;
import com.android.settings.flashlight.FlashlightSliceBuilder;
import com.android.settings.notification.ZenModeSliceBuilder;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.wifi.WifiSliceBuilder;
@@ -102,6 +104,9 @@ public class SliceBroadcastReceiver extends BroadcastReceiver {
                        .getNewWifiCallingSliceHelper(context)
                        .handleWifiCallingPreferenceChanged(intent);
                break;
            case ACTION_FLASHLIGHT_SLICE_CHANGED:
                FlashlightSliceBuilder.handleUriChange(context, intent);
                break;
            default:
                final String uriString = intent.getStringExtra(SliceBroadcastRelay.EXTRA_URI);
                if (!TextUtils.isEmpty(uriString)) {
+81 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 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.flashlight;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;

import android.content.Context;

import com.android.settings.R;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.SliceTester;

import android.content.res.Resources;
import android.provider.Settings;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RuntimeEnvironment;

import java.util.List;

import androidx.slice.Slice;
import androidx.slice.SliceItem;
import androidx.slice.SliceMetadata;
import androidx.slice.SliceProvider;
import androidx.slice.core.SliceAction;
import androidx.slice.widget.SliceLiveData;


@RunWith(SettingsRobolectricTestRunner.class)
public class FlashlightSliceBuilderTest {

    private Context mContext;

    @Before
    public void setUp() {
        mContext = spy(RuntimeEnvironment.application);

        // Prevent crash in SliceMetadata.
        Resources resources = spy(mContext.getResources());
        doReturn(60).when(resources).getDimensionPixelSize(anyInt());
        doReturn(resources).when(mContext).getResources();

        // Set-up specs for SliceMetadata.
        SliceProvider.setSpecs(SliceLiveData.SUPPORTED_SPECS);
    }

    @Test
    public void getFlashlightSlice_correctData() {
        Settings.Secure.putInt(
                mContext.getContentResolver(), Settings.Secure.FLASHLIGHT_AVAILABLE, 1);
        final Slice slice = FlashlightSliceBuilder.getSlice(mContext);
        final SliceMetadata metadata = SliceMetadata.from(mContext, slice);

        final List<SliceAction> toggles = metadata.getToggles();
        assertThat(toggles).hasSize(1);

        final List<SliceItem> sliceItems = slice.getItems();
        SliceTester.assertTitle(sliceItems, mContext.getString(R.string.power_flashlight));
    }
}
Loading