summaryrefslogtreecommitdiffstats
path: root/src/com/android/camera/TsMakeupManager.java
blob: f4e113e9bd5342615d2a07a734e6a8edd561b974 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*
* Copyright (C) 2014,2015 Thundersoft Corporation
* All rights Reserved
*
* 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.camera;

import android.content.Context;
import android.content.res.Resources;
import android.text.TextUtils;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;

import com.android.camera.ui.RotateLayout;
import com.android.camera.ui.RotateTextToast;
import com.android.camera.util.CameraUtil;

import org.codeaurora.snapcam.R;

public class TsMakeupManager implements OnSeekBarChangeListener {
    private static final String TAG = "TsMakeupManager";
    private PhotoUI mUI;
    private PhotoMenu mMenu;
    private CameraActivity mActivity;
    private PreferenceGroup mPreferenceGroup;
    private View mTsMakeupSwitcher;

    private RelativeLayout mMakeupLayoutRoot;
    private LinearLayout mMakeupLevelRoot;
    private LinearLayout mMakeupSingleRoot;

    public static final String MAKEUP_ON     = "On";
    public static final String MAKEUP_OFF    = "Off";
    public static final String MAKEUP_NONE   = "none";

    private static final int MODE_NONE = 0;
    private static final int MODE_WHITEN = 1;
    private static final int MODE_CLEAN = 2;
    private int mMode = MODE_NONE;
    private int mSingleSelectedIndex = MODE_NONE;

    private static final int MAKEUP_UI_STATUS_NONE      = 0;
    private static final int MAKEUP_UI_STATUS_ON        = 1;
    private static final int MAKEUP_UI_STATUS_OFF       = 2;
    private static final int MAKEUP_UI_STATUS_DISMISS   = 3;
    private int mMakeupUIStatus = MAKEUP_UI_STATUS_NONE;

    private static final int CLICK_THRESHOLD = 200;

    public static final boolean HAS_TS_MAKEUP = android.os.SystemProperties.getBoolean("persist.ts.rtmakeup", false);

    private MakeupLevelListener mMakeupLevelListener;
    interface MakeupLevelListener {
        void onMakeupLevel(String key, String value);
    }

    public void setMakeupLevelListener(MakeupLevelListener l) {
        mMakeupLevelListener = l;
    }

    public TsMakeupManager(CameraActivity activity, PhotoMenu menu, PhotoUI ui, PreferenceGroup preferenceGroup, View makeupSwitcher) {
        mActivity = activity;
        mUI = ui;
        mMenu = menu;
        mPreferenceGroup = preferenceGroup;
        mTsMakeupSwitcher = makeupSwitcher;

        mMakeupLayoutRoot = (RelativeLayout) mUI.getRootView().findViewById(R.id.id_tsmakeup_level_layout_root);

        mMakeupUIStatus = MAKEUP_UI_STATUS_NONE;
    }

    public View getMakeupLayoutRoot() {
        return mMakeupLayoutRoot;
    }

    public boolean isShowMakeup() {
        return mMakeupLayoutRoot != null && mMakeupLayoutRoot.isShown();
    }

    public void removeAllViews() {
        if(mMakeupSingleRoot != null) {
            mMakeupSingleRoot.removeAllViews();
            mMakeupSingleRoot = null;
        }
        if(mMakeupLevelRoot != null) {
            mMakeupLevelRoot.removeAllViews();
            mMakeupLevelRoot = null;
        }
        if(mMakeupLayoutRoot != null) {
            mMakeupLayoutRoot.removeAllViews();
        }
    }

    public void dismissMakeupUI() {
        mMakeupUIStatus = MAKEUP_UI_STATUS_DISMISS;
        removeAllViews();
        if(mMakeupLayoutRoot != null) {
            mMakeupLayoutRoot.setVisibility(View.GONE);
        }
    }

    public void resetMakeupUIStatus() {
        mMakeupUIStatus = MAKEUP_UI_STATUS_NONE;
    }

    private void changeMakeupIcon(String value) {
        if( !TextUtils.isEmpty(value) ) {
            String prefValue = MAKEUP_ON;
            if(MAKEUP_OFF.equals(value)) {
                prefValue = MAKEUP_OFF;
            }
            final IconListPreference pref = (IconListPreference) mPreferenceGroup
                    .findPreference(CameraSettings.KEY_TS_MAKEUP_UILABLE);
            if(pref == null)
                return;
            pref.setValue(prefValue);
            int index = pref.getCurrentIndex();
            ImageView iv = (ImageView) mTsMakeupSwitcher;
            iv.setImageResource(((IconListPreference) pref).getLargeIconIds()[index]);
            pref.setMakeupSeekBarValue(prefValue);
        }
    }

    public void hideMakeupUI() {
        final IconListPreference pref = (IconListPreference) mPreferenceGroup
                .findPreference(CameraSettings.KEY_TS_MAKEUP_UILABLE);
        if(pref == null)
            return;
        mMakeupUIStatus = MAKEUP_UI_STATUS_NONE;
        String tsMakeupOn = pref.getValue();
        Log.d(TAG, "TsMakeupManager.hideMakeupUI(): tsMakeupOn is " + tsMakeupOn);
        if(MAKEUP_ON.equals(tsMakeupOn)) {
            int index = pref.findIndexOfValue(pref.getValue());
            CharSequence[] values = pref.getEntryValues();
            index = (index + 1) % values.length;
            pref.setMakeupSeekBarValue((String)values[index]);
            ImageView iv = (ImageView) mTsMakeupSwitcher;
            iv.setImageResource(((IconListPreference) pref).getLargeIconIds()[index]);
            mMakeupLevelListener.onMakeupLevel(CameraSettings.KEY_TS_MAKEUP_LEVEL, pref.getValue());

            IconListPreference levelPref = (IconListPreference) mPreferenceGroup
                    .findPreference(CameraSettings.KEY_TS_MAKEUP_LEVEL);
            levelPref.setValueIndex(0); //Turn Off the Makeup feature;


            mMakeupLayoutRoot.setVisibility(View.GONE);
            mMakeupLayoutRoot.removeAllViews();
            if(mMakeupSingleRoot != null) {
                mMakeupSingleRoot.removeAllViews();
                mMakeupSingleRoot = null;
            }
            if(mMakeupLevelRoot != null) {
                mMakeupLevelRoot.removeAllViews();
                mMakeupLevelRoot = null;
            }
        }
    }

    public void showMakeupView() {
        mMakeupUIStatus = MAKEUP_UI_STATUS_OFF;
        mMakeupLayoutRoot.setVisibility(View.GONE);
        mMakeupLayoutRoot.removeAllViews();
        if(mMakeupSingleRoot != null) {
            mMakeupSingleRoot.removeAllViews();
            mMakeupSingleRoot = null;
        }
        if(mMakeupLevelRoot != null) {
            mMakeupLevelRoot.removeAllViews();
            mMakeupLevelRoot = null;
        }

        if(mMakeupSingleRoot != null && mMakeupSingleRoot.getVisibility() == View.VISIBLE) {
            showSingleView(MAKEUP_NONE);
            return;
        }

        if(mMakeupUIStatus == MAKEUP_UI_STATUS_DISMISS)
            return;

        mMakeupLayoutRoot.setVisibility(View.VISIBLE);
        final IconListPreference pref = (IconListPreference) mPreferenceGroup
                .findPreference(CameraSettings.KEY_TS_MAKEUP_LEVEL);
        if (pref == null)
            return;

        if(mMakeupLevelRoot != null) {
            mMakeupLevelRoot.removeAllViews();
            mMakeupLevelRoot = null;
        }
        mMakeupLayoutRoot.removeAllViews();

        mMakeupUIStatus = MAKEUP_UI_STATUS_ON;

        int rotation = CameraUtil.getDisplayRotation(mActivity);
        boolean mIsDefaultToPortrait = CameraUtil.isDefaultToPortrait(mActivity);
        if (!mIsDefaultToPortrait) {
            rotation = (rotation + 90) % 360;
        }
        CharSequence[] entries = pref.getEntries();
        int[] thumbnails = pref.getThumbnailIds();

        WindowManager wm = (WindowManager) mActivity.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        int width = display.getWidth();
        int height = display.getHeight();
        Resources r = mActivity.getResources();
        int margin = (int) (r.getDimension(R.dimen.tsmakeup_mode_paddingBottom));
        int levelBgSize = (int) (r.getDimension(R.dimen.tsmakeup_mode_level_size));

        Log.d(TAG, "TsMakeupManager.showMakeupView(): rotation is " + rotation + ", WH is (" + width + ", " + height + "), margin is "
                + margin + ", levelBgSize is " + levelBgSize);

        int gridRes = 0;
        boolean portrait = (rotation == 0) || (rotation == 180);
        int size = height;
        if (portrait) {
            gridRes = R.layout.ts_makeup_level_view_port;
            size = width;
        } else {
            gridRes = R.layout.ts_makeup_level_view_land;
            size = height;
        }
        int itemWidth = size / entries.length;

        LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        LinearLayout layout = (LinearLayout) inflater.inflate(gridRes, null, false);
        mMakeupLevelRoot = layout;
        mUI.setMakeupMenuLayout(layout);

        LinearLayout.LayoutParams params = null;
        if(portrait) {
            params = new LayoutParams(itemWidth, itemWidth);
            params.gravity = Gravity.CENTER_VERTICAL;
        } else {
            params = new LayoutParams(itemWidth, itemWidth);
            params.gravity = Gravity.CENTER_HORIZONTAL;
        }

        RelativeLayout.LayoutParams rootParams = null;
        if(rotation == 0) {
            rootParams = new RelativeLayout.LayoutParams(size, levelBgSize);
//            rootParams.bottomMargin = margin;
            rootParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        } else if(rotation == 90) {
            rootParams = new RelativeLayout.LayoutParams(levelBgSize, size);
//            rootParams.rightMargin = margin;
            rootParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        } else if(rotation == 180) {
            rootParams = new RelativeLayout.LayoutParams(size, levelBgSize);
//            rootParams.topMargin = margin;
            rootParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        } else if(rotation == 270) {
            rootParams = new RelativeLayout.LayoutParams(levelBgSize, size);
//            rootParams.leftMargin = margin;
            rootParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        }

        final View[] views = new View[entries.length];
        int init = pref.getCurrentIndex();
        for (int i = 0; i < entries.length; i++) {
            RotateLayout layout2 = (RotateLayout) inflater.inflate(
                    R.layout.ts_makeup_item_view, null, false);

            ImageView imageView = (ImageView) layout2.findViewById(R.id.image);
            TextView label = (TextView) layout2.findViewById(R.id.label);
            final int j = i;

            layout2.setOnTouchListener(new View.OnTouchListener() {
                private long startTime;

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        startTime = System.currentTimeMillis();
                    } else if (event.getAction() == MotionEvent.ACTION_UP) {
                        if (System.currentTimeMillis() - startTime < CLICK_THRESHOLD) {
                            pref.setValueIndex(j);
                            changeMakeupIcon(pref.getValue());
                            mMakeupLevelListener.onMakeupLevel(pref.getKey(), pref.getValue());
                            for (View v1 : views) {
                                v1.setSelected(false);
                            }
                            View border = v.findViewById(R.id.image);
                            border.setSelected(true);

                            showSingleView(pref.getValue());
                            mUI.adjustOrientation();
                            if(!pref.getValue().equalsIgnoreCase("off")) {
                                String toast = mActivity.getString(
                                        R.string.text_tsmakeup_beautify_toast);
                                RotateTextToast.makeText(mActivity, toast, Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                    return true;
                }
            });

            View border = layout2.findViewById(R.id.image);
            views[j] = border;
            if (i == init) {
                border.setSelected(true);
            }
            imageView.setImageResource(thumbnails[i]);
            label.setText(entries[i]);
            layout.addView(layout2, params);
        }
        mMakeupLayoutRoot.addView(layout, rootParams);
    }

    private void showSingleView(String value) {
        if(MAKEUP_NONE.equals(value)) {
            if(mMakeupSingleRoot != null) {
                mMakeupSingleRoot.removeAllViews();
                mMakeupSingleRoot = null;
            }
            mMakeupLayoutRoot.removeAllViews();
            int rotation = CameraUtil.getDisplayRotation(mActivity);
            boolean mIsDefaultToPortrait = CameraUtil.isDefaultToPortrait(mActivity);
            if (!mIsDefaultToPortrait) {
                rotation = (rotation + 90) % 360;
            }

            WindowManager wm = (WindowManager) mActivity.getSystemService(Context.WINDOW_SERVICE);
            Display display = wm.getDefaultDisplay();
            int width = display.getWidth();
            int height = display.getHeight();
            Resources r = mActivity.getResources();
            int margin = (int) (r.getDimension(R.dimen.tsmakeup_mode_paddingBottom));
            int levelBgSize = (int) (r.getDimension(R.dimen.tsmakeup_mode_level_size));

            Log.d(TAG, "TsMakeupManager.showSingleView(): rotation is " + rotation + ", WH is (" + width + ", " + height + "), margin is "
                    + margin + ", levelBgSize is " + levelBgSize);

            int gridRes = R.layout.ts_makeup_single_level_view_port;
            int size = width;

            LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            final LinearLayout layout = (LinearLayout) inflater.inflate(gridRes, null, false);
            mMakeupSingleRoot = layout;
            mUI.setMakeupMenuLayout(layout);

            RelativeLayout.LayoutParams rootParams = new RelativeLayout.LayoutParams(size, android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
            rootParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

            mMakeupLayoutRoot.addView(layout, rootParams);
            final SeekBar seekBar = (SeekBar) layout.findViewById(R.id.seekbar_makeup_level);
            seekBar.setOnSeekBarChangeListener(this);
            setSingleView(layout);

            mMode = MODE_NONE;

            layout.findViewById(R.id.id_layout_makeup_back).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    mMakeupSingleRoot.removeAllViews();
                    mMakeupLayoutRoot.removeView(mMakeupSingleRoot);
                    mMakeupSingleRoot = null;

                    mSingleSelectedIndex = MODE_NONE;
                    mMode = MODE_NONE;

                    showMakeupView();
                    mUI.adjustOrientation();
                }
            });

            layout.findViewById(R.id.id_layout_makeup_whiten).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mMode == MODE_WHITEN) {
                        seekBar.setVisibility(View.GONE);
                        mMode = MODE_NONE;
                        return;
                    }
                    mSingleSelectedIndex = MODE_WHITEN;
                    seekBar.setVisibility(View.VISIBLE);
                    seekBar.setProgress(getPrefValue(CameraSettings.KEY_TS_MAKEUP_LEVEL_WHITEN));
                    mMode = MODE_WHITEN;
                    setSingleView(layout);
                }
            });

            layout.findViewById(R.id.id_layout_makeup_clean).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mMode == MODE_CLEAN) {
                        seekBar.setVisibility(View.GONE);
                        mMode = MODE_NONE;
                        return;
                    }
                    mSingleSelectedIndex = MODE_CLEAN;
                    seekBar.setVisibility(View.VISIBLE);
                    seekBar.setProgress(getPrefValue(CameraSettings.KEY_TS_MAKEUP_LEVEL_CLEAN));
                    mMode = MODE_CLEAN;
                    setSingleView(layout);
                }
            });
        }
    }

    private void setSingleView(LinearLayout layout) {
        if(mSingleSelectedIndex == MODE_WHITEN) {
            layout.findViewById(R.id.id_iv_makeup_whiten).setSelected(true);
            layout.findViewById(R.id.id_iv_makeup_clean).setSelected(false);
        } else if(mSingleSelectedIndex == MODE_CLEAN) {
            layout.findViewById(R.id.id_iv_makeup_whiten).setSelected(false);
            layout.findViewById(R.id.id_iv_makeup_clean).setSelected(true);
        }
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        setSeekbarValue(seekBar.getProgress());
    }

    private void setSeekbarValue(int value) {
        String key = CameraSettings.KEY_TS_MAKEUP_LEVEL_WHITEN;
        if(mMode == MODE_CLEAN) {
            key = CameraSettings.KEY_TS_MAKEUP_LEVEL_CLEAN;
        }
        Log.d(TAG, "TsMakeupManager.onStopTrackingTouch(): value is " + value + ", key is " + key);
        setEffectValue(key, String.valueOf(value));
    }

    private void setEffectValue(String key, String value) {
        final ListPreference pref = (ListPreference) mPreferenceGroup.findPreference(key);
        if (pref == null)
            return;

        pref.setMakeupSeekBarValue(value);
        mMakeupLevelListener.onMakeupLevel(key, value);
    }

    private int getPrefValue(String key) {
        ListPreference pref = mPreferenceGroup.findPreference(key);
        String value = pref.getValue();
        Log.d(TAG, "TsMakeupManager.getPrefValue(): value is " + value + ", key is " + key);
        if(TextUtils.isEmpty(value)) {
            value = mActivity.getString(R.string.pref_camera_tsmakeup_level_default);
        }
        return Integer.parseInt(value);
    }
}