summaryrefslogtreecommitdiffstats
path: root/services/java/com/android/server/status/widget/FlashlightButton.java
blob: eb25cf0663bcb70e999ac56a07e2d5014321271b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package com.android.server.status.widget;

import com.android.internal.R;
import com.android.server.status.widget.PowerButton;

import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import android.util.Log;
import android.os.Build;

import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.lang.Thread;
import java.lang.Runnable;
import java.io.File;
import java.io.FileWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class FlashlightButton extends PowerButton {

    Context mContext;

    static FlashlightButton ownButton;
    private static final String TAG = "FlashlightButton";
    private static int currentMode;

    private static FileWriter mWriter;

    private static ExecutorService threadExecutor;
    private static boolean runTimer = false;

    private static final int MODE_DEFAULT = 0;
    private static final int MODE_HIGH = 1;
    private static boolean useDeathRay = !Build.DEVICE.equals("supersonic");;

    private static final String FLASHLIGHT_FILE;
    private static final String FLASHLIGHT_FILE_SPOTLIGHT = "/sys/class/leds/spotlight/brightness";
    static {
        File ff = new File(FLASHLIGHT_FILE_SPOTLIGHT);
        if (ff.exists()) {
            FLASHLIGHT_FILE = FLASHLIGHT_FILE_SPOTLIGHT;
        } else {
            FLASHLIGHT_FILE = "/sys/class/leds/flashlight/brightness";
        }
    }

    public void updateState(Context context) {
        mContext = context;
        boolean useCustomExp = Settings.System.getInt(mContext.getContentResolver(),
        Settings.System.NOTIF_EXPANDED_BAR_CUSTOM, 0) == 1;

        if(getFlashlightEnabled()) {
            if (useCustomExp) {
                currentIcon = com.android.internal.R.drawable.stat_flashlight_on_cust;
            } else {
                currentIcon = com.android.internal.R.drawable.stat_flashlight_on;
            }
            currentState = STATE_ENABLED;
        } else {
            if (useCustomExp) {
                currentIcon = com.android.internal.R.drawable.stat_flashlight_off_cust;
            } else {
                currentIcon = com.android.internal.R.drawable.stat_flashlight_off;
            }
            currentState = STATE_DISABLED;
        }
    }

    public void toggleState(Context context) {
        currentMode = Settings.System.getInt(context.getContentResolver(), Settings.System.EXPANDED_FLASH_MODE,
                    MODE_DEFAULT);
        boolean enabled = getFlashlightEnabled();
        runTimer = !enabled;
        setFlashlightEnabled(!enabled);
    }

    public static FlashlightButton getInstance() {
        if (ownButton==null) ownButton = new FlashlightButton();

        return ownButton;
    }


    public boolean getFlashlightEnabled() {
        try {
            FileInputStream fis = new FileInputStream(FLASHLIGHT_FILE);
            int result = fis.read();
            fis.close();
            return (result != '0');
        } catch (Exception e) {
            return false;
        }
    }

    public void setFlashlightEnabled(boolean on) {
        try {
            if (mWriter == null) {
                mWriter = new FileWriter(FLASHLIGHT_FILE);
            }
            int value = 0;
            if (on) {
                switch (currentMode) {
                    case MODE_HIGH:
                        value = useDeathRay ? 3 : 128;
                        break;
                    default:
                        value = 1;
                        break;
                }
            }
            mWriter.write(String.valueOf(value));
            mWriter.flush();
            if (!on) {
                mWriter.close();
                mWriter = null;
            }
        } catch (Exception e) {
            Log.e(TAG, "setFlashlightEnabled failed", e);
        }
        startTimer(on);
    }

    private void startTimer(boolean on) {
        if (!on) return;
        if (threadExecutor == null) threadExecutor = Executors.newSingleThreadExecutor();

        flashTimer timerRun = new flashTimer();
        threadExecutor.execute(timerRun);
    }

    public class flashTimer implements Runnable {
        public flashTimer() {
        }
        public void run() {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
            }
            setFlashlightEnabled(runTimer);
        }
    }


    @Override
    void initButton(int position) {
    }

}