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

Commit 1621c284 authored by Danny Baumann's avatar Danny Baumann
Browse files

Use sticky broadcast to convey torch state (1/2).

It doesn't really make sense to store the (non-persistent) state of the
torch in a (persistent) setting. As the torch is sending a broadcast
anyway, make that broadcast sticky and use it to convey the state.

Also unify the torch constants (actions, extras, activity names) at a
common place.

Change-Id: If845bef27cc79990794aa748dce18cfea3bac1f5
parent 3754b942
Loading
Loading
Loading
Loading
+0 −6
Original line number Diff line number Diff line
@@ -2504,12 +2504,6 @@ public final class Settings {
         */
        public static final String SIP_ASK_ME_EACH_TIME = "SIP_ASK_ME_EACH_TIME";

        /**
         * Torch state (flashlight)
         * FIXME: move me to global state
         * @hide
         */
        public static final String TORCH_STATE = "torch_state";
        /**
         * Pointer speed setting.
         * This is an integer value in a range between -7 and +7, so there are 15 possible values.
+9 −3
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
@@ -33,6 +34,7 @@ import android.provider.Settings;
import android.text.TextUtils;

import static com.android.internal.util.cm.NavigationRingConstants.*;
import com.android.internal.util.cm.TorchConstants;
import com.android.internal.widget.multiwaveview.GlowPadView;
import com.android.internal.widget.multiwaveview.TargetDrawable;

@@ -43,6 +45,9 @@ public class NavigationRingHelpers {

    private static final String ASSIST_ICON_METADATA_NAME = "com.android.systemui.action_assist_icon";

    private static final IntentFilter TORCH_STATE_FILTER =
            new IntentFilter(TorchConstants.ACTION_STATE_CHANGED);

    private NavigationRingHelpers() {
    }

@@ -94,7 +99,7 @@ public class NavigationRingHelpers {
    public static boolean isTorchAvailable(Context context) {
        PackageManager pm = context.getPackageManager();
        try {
            return pm.getPackageInfo("net.cactii.flash2", 0) != null;
            return pm.getPackageInfo(TorchConstants.APP_PACKAGE_NAME, 0) != null;
        } catch (PackageManager.NameNotFoundException e) {
            // ignored, just catched so we can return false below
        }
@@ -204,8 +209,9 @@ public class NavigationRingHelpers {
    }

    private static int getTorchDrawableResId(Context context) {
        boolean active = Settings.System.getInt(context.getContentResolver(),
                Settings.System.TORCH_STATE, 0) != 0;
        Intent stateIntent = context.registerReceiver(null, TORCH_STATE_FILTER);
        boolean active = stateIntent != null
                && stateIntent.getIntExtra(TorchConstants.EXTRA_CURRENT_STATE, 0) != 0;

        if (active) {
            return com.android.internal.R.drawable.ic_navigation_ring_torch_on;
+56 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2013 The CyanogenMod 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.internal.util.cm;

import android.content.Intent;

public class TorchConstants {
    /**
     * Package name of the torch app
     */
    public static final String APP_PACKAGE_NAME = "net.cactii.flash2";

    /**
     * Intent broadcast action for toggling the torch state
     */
    public static final String ACTION_TOGGLE_STATE = APP_PACKAGE_NAME + ".TOGGLE_FLASHLIGHT";

    /**
     * Extra for {@link ACTION_TOGGLE_STATE}:
     * When toggling to on, use the bright brightness setting
     * Type: boolean
     */
    public static final String EXTRA_BRIGHT_MODE = "bright";

    /**
     * Intent action for 'torch state changed' broadcast
     */
    public static final String ACTION_STATE_CHANGED = APP_PACKAGE_NAME + ".TORCH_STATE_CHANGED";

    /**
     * Extra for {@link ACTION_STATE_CHANGED}:
     * Current torch state
     * Type: integer (0/1)
     */
    public static final String EXTRA_CURRENT_STATE = "state";

    /**
     * Intent for launching the torch application
     */
    public static Intent INTENT_LAUNCH_APP = new Intent(Intent.ACTION_MAIN)
            .setClassName(APP_PACKAGE_NAME, APP_PACKAGE_NAME + ".MainActivity");
}
+2 −1
Original line number Diff line number Diff line
@@ -44,6 +44,7 @@ import android.view.KeyEvent;
import android.widget.Toast;

import com.android.internal.statusbar.IStatusBarService;
import com.android.internal.util.cm.TorchConstants;
import com.android.internal.R;
import static com.android.internal.util.cm.NavigationRingConstants.*;
import com.android.systemui.screenshot.TakeScreenshotService;
@@ -146,7 +147,7 @@ public class ActionTarget {
            }
            return true;
        } else if (action.equals(ACTION_TORCH)) {
            Intent intent = new Intent("net.cactii.flash2.TOGGLE_FLASHLIGHT");
            Intent intent = new Intent(TorchConstants.ACTION_TOGGLE_STATE);
            mContext.sendBroadcast(intent);
            return true;
        } else {
+8 −10
Original line number Diff line number Diff line
@@ -10,11 +10,13 @@ import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;

import com.android.internal.util.cm.TorchConstants;
import com.android.systemui.R;
import com.android.systemui.statusbar.phone.QuickSettingsContainerView;
import com.android.systemui.statusbar.phone.QuickSettingsController;

public class TorchTile extends QuickSettingsTile {
    private boolean mActive = false;

    public TorchTile(Context context, 
            QuickSettingsController qsc, Handler handler) {
@@ -23,7 +25,7 @@ public class TorchTile extends QuickSettingsTile {
        mOnClick = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent("net.cactii.flash2.TOGGLE_FLASHLIGHT");
                Intent i = new Intent(TorchConstants.ACTION_TOGGLE_STATE);
                mContext.sendBroadcast(i);
            }
        };
@@ -31,14 +33,12 @@ public class TorchTile extends QuickSettingsTile {
        mOnLongClick = new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.setClassName("net.cactii.flash2", "net.cactii.flash2.MainActivity");
                startSettingsActivity(intent);
                startSettingsActivity(TorchConstants.INTENT_LAUNCH_APP);
                return true;
            }
        };

        qsc.registerObservedContent(Settings.System.getUriFor(Settings.System.TORCH_STATE), this);
        qsc.registerAction(TorchConstants.ACTION_STATE_CHANGED, this);
    }

    @Override
@@ -54,10 +54,7 @@ public class TorchTile extends QuickSettingsTile {
    }

    private synchronized void updateTile() {
        boolean enabled = Settings.System.getIntForUser(mContext.getContentResolver(),
                Settings.System.TORCH_STATE, 0, UserHandle.USER_CURRENT) == 1;

        if (enabled) {
        if (mActive) {
            mDrawable = R.drawable.ic_qs_torch_on;
            mLabel = mContext.getString(R.string.quick_settings_torch);
        } else {
@@ -67,7 +64,8 @@ public class TorchTile extends QuickSettingsTile {
    }

    @Override
    public void onChangeUri(ContentResolver resolver, Uri uri) {
    public void onReceive(Context context, Intent intent) {
        mActive = intent.getIntExtra(TorchConstants.EXTRA_CURRENT_STATE, 0) != 0;
        updateResources();
    }
}
Loading