summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings/CustomScreenColor.java
blob: 2d6c7c2346e2a96a719305a00efd0349053ea870 (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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
/*
   Copyright (c) 2014, The Linux Foundation. All Rights Reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials provided
      with the distribution.
    * Neither the name of The Linux Foundation nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.android.settings;

import android.app.ActionBar;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.PopupWindow;

import com.android.display.IPPService;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class CustomScreenColor extends Activity implements SeekBar.OnSeekBarChangeListener,
        View.OnClickListener {
    private final static String TAG = "CustomScreenColor";

    private static final int RESTORE_DEFAULT_PREVIEW = 0;
    private static final int SELECT_NEW_FILE_ITEM = 1;
    private static final int SELECT_FILE_ITEM = 2;
    private static final int RESET = 3;
    private static final int SELECT_FILE_GROUP1 = 0;
    private static final int SELECT_FILE_GROUP2 = 1;
    private static final int SELECT_FILE_ORDER = 0;
    private static final int REQUEST_SELECT_FILE = 100;
    private static final String IMAGE_UNSPECIFIED = "image/*";
    private static final String PREVIEW_STRING_KEY = "screencolor_preview_key";
    private static final String PREVIEW_STRING_NAME = "screencolor_preview_name";
    private static final String COLOR_HUE = "hue";
    private static final String COLOR_SATURATION = "saturation";
    private static final String COLOR_INTENSITY = "intensity";
    private static final String COLOR_CONTRAST = "contrast";

    private static final String KEY_ASPECT_X = "aspectX";
    private static final String KEY_ASPECT_Y = "aspectY";
    private static final String KEY_SPOTLIGHT_X = "spotlightX";
    private static final String KEY_SPOTLIGHT_Y = "spotlightY";
    private static final String KEY_FROME_SCREENCOLOR = "fromScreenColor";

    private ImageView mImageView;
    private RelativeLayout mRLayout;
    private LinearLayout mScreenColorLayout;
    private SeekBar mHBar, mSBar, mCBar, mIBar;
    private TextView mHTv, mSTv, mCTv, mITv;
    private ImageView mReduceH, mIncreaseH, mReduceS, mIncreaseS, mReduceC, mIncreaseC, mReduceI,
            mIncreaseI, mUpdown, mMore;
    private Button mCancelBtn, mSaveBtn, mPreviousBtn, mNewBtn;
    private boolean canRestorePreview;
    private int mHueValue = 0;
    private int mSaturationValue = 0;
    private int mIntensityValue = 0;
    private int mContrastValue = 0;

    private int mMinHueValue = 0;
    private int mMaxHueValue = 0;
    private int mDefaultHueValue = 0;
    private int mMinSaturationValue = 0;
    private int mMaxSaturationValue = 0;
    private int mDefaultSaturationValue = 0;
    private int mMinIntensityValue = 0;
    private int mMaxIntensityValue = 0;
    private int mDefaultIntensityValue = 0;
    private int mMinContrastValue = 0;
    private int mMaxContrastValue = 0;
    private int mDefaultContrastValue = 0;

    /**
     *  It is used to update the screen's hue, saturation, contrast, and intensity.
     */
    IPPService mPPService = null;
    PPServiceConnection mPPServiceConn = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initPPService();
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
        setContentView(R.layout.custom_screencolor);
        initView();
        restoreSavedHSCI(true);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(mPPServiceConn);
    }

    private void initView() {
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        mMinHueValue = getResources().getInteger(R.integer.minimum_hue_value);
        mMaxHueValue = getResources().getInteger(R.integer.maximum_hue_value);
        mDefaultHueValue = getResources().getInteger(R.integer.default_hue_value);
        mMinSaturationValue = getResources().getInteger(R.integer.minimum_saturation_value);
        mMaxSaturationValue = getResources().getInteger(R.integer.maximum_saturation_value);
        mDefaultSaturationValue = getResources().getInteger(R.integer.default_saturation_value);
        mMinIntensityValue = getResources().getInteger(R.integer.minimum_intensity_value);
        mMaxIntensityValue = getResources().getInteger(R.integer.maximum_intensity_value);
        mDefaultIntensityValue = getResources().getInteger(R.integer.default_intensity_value);
        mMinContrastValue = getResources().getInteger(R.integer.minimum_contrast_value);
        mMaxContrastValue = getResources().getInteger(R.integer.maximum_contrast_value);
        mDefaultContrastValue = getResources().getInteger(R.integer.default_contrast_value);

        mRLayout = (RelativeLayout) findViewById(R.id.background_preview);
        String previewContent = getScreenColorPreviewKey();
        if ("".equals(previewContent)) {
            resotreBackgroundByDefault();
        } else {
            Uri previewUri = Uri.parse(previewContent);
            setBackgroundByUri(previewUri);
        }

        mScreenColorLayout = (LinearLayout) findViewById(R.id.screencolor_control);
        mScreenColorLayout.setBackgroundColor(R.color.screencolor_background);
        mScreenColorLayout.setOnClickListener(this);

        mHBar = (SeekBar) findViewById(R.id.hcontrol);
        mHBar.setMax(mMaxHueValue);
        mHBar.setOnSeekBarChangeListener(this);
        mSBar = (SeekBar) findViewById(R.id.scontrol);
        mSBar.setMax(mMaxSaturationValue);
        mSBar.setOnSeekBarChangeListener(this);
        mIBar = (SeekBar) findViewById(R.id.icontrol);
        mIBar.setMax(mMaxIntensityValue);
        mIBar.setOnSeekBarChangeListener(this);
        mCBar = (SeekBar) findViewById(R.id.ccontrol);
        mCBar.setMax(mMaxContrastValue);
        mCBar.setOnSeekBarChangeListener(this);

        mHTv = (TextView) findViewById(R.id.hue);
        mHTv.setText(getString(R.string.hue_str, mHBar.getProgress()));
        mSTv = (TextView) findViewById(R.id.saturation);
        mSTv.setText(getString(R.string.saturation_str, mSBar.getProgress()));
        mITv = (TextView) findViewById(R.id.intensity);
        mITv.setText(getString(R.string.intensity_str, mIBar.getProgress()));
        mCTv = (TextView) findViewById(R.id.contrast);
        mCTv.setText(getString(R.string.contrast_str, mCBar.getProgress()));

        mReduceH = (ImageView) findViewById(R.id.reduce_hue);
        mReduceH.setOnClickListener(this);
        mIncreaseH = (ImageView) findViewById(R.id.increase_hue);
        mIncreaseH.setOnClickListener(this);
        mReduceS = (ImageView) findViewById(R.id.reduce_saturation);
        mReduceS.setOnClickListener(this);
        mIncreaseS = (ImageView) findViewById(R.id.increase_saturation);
        mIncreaseS.setOnClickListener(this);
        mReduceI = (ImageView) findViewById(R.id.reduce_intensity);
        mReduceI.setOnClickListener(this);
        mIncreaseI = (ImageView) findViewById(R.id.increase_intensity);
        mIncreaseI.setOnClickListener(this);
        mReduceC = (ImageView) findViewById(R.id.reduce_contrast);
        mReduceC.setOnClickListener(this);
        mIncreaseC = (ImageView) findViewById(R.id.increase_contrast);
        mIncreaseC.setOnClickListener(this);

        mUpdown = (ImageView) findViewById(R.id.up_down);
        mUpdown.setOnClickListener(this);
        mMore = (ImageView) findViewById(R.id.more);
        mMore.setOnClickListener(this);

        mPreviousBtn = (Button) findViewById(R.id.previous_btn);
        mPreviousBtn.setOnClickListener(this);
        mNewBtn = (Button) findViewById(R.id.new_btn);
        mNewBtn.setOnClickListener(this);
        mCancelBtn = (Button) findViewById(R.id.cancel);
        mCancelBtn.setOnClickListener(this);
        mSaveBtn = (Button) findViewById(R.id.save);
        mSaveBtn.setOnClickListener(this);

        initBtnsStatus();
    }

    private void initPPService() {
        mPPServiceConn = new PPServiceConnection();
        String packetname = "com.qualcomm.display";
        String service = "com.qualcomm.display.PPService";
        Intent i = new Intent(IPPService.class.getName());

        ComponentName component = new ComponentName(packetname, service);
        i.setComponent(component);

        boolean ret = bindService(i, mPPServiceConn, Context.BIND_AUTO_CREATE);
    }

    private class PPServiceConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mPPService = IPPService.Stub.asInterface((IBinder) service);
            Log.d(TAG, "onServiceConnected, service=" + mPPService);
            try {
                if (null != mPPService) {
                    mPPService.startPP();
                    restoreSavedHSCI(true);
                }
            } catch (RemoteException e) {
                Log.e(TAG, "startPP exception");
            }

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            if (null != mPPService) {
                // end thread
                try {
                    Log.d(TAG, "stopPP");
                    mPPService.stopPP();
                } catch (RemoteException e) {
                    Log.e(TAG, "stopPP exception");
                }
            }
            mPPService = null;
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_OK) {
            Log.i(TAG, "bail due to resultCode=" + resultCode);
            return;
        }
        switch (requestCode) {
            case REQUEST_SELECT_FILE:
                changePreviewByData(data);
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(SELECT_FILE_GROUP1, SELECT_FILE_ITEM, SELECT_FILE_ORDER,
                R.string.selectfile_menu);
        menu.add(SELECT_FILE_GROUP1, RESET, SELECT_FILE_ORDER, R.string.restore_default_str);
        menu.add(SELECT_FILE_GROUP2, RESTORE_DEFAULT_PREVIEW, SELECT_FILE_ORDER,
                R.string.restore_preview);
        menu.add(SELECT_FILE_GROUP2, SELECT_NEW_FILE_ITEM, SELECT_FILE_ORDER,
                R.string.selectnewfile_menu);
        menu.add(SELECT_FILE_GROUP2, RESET, SELECT_FILE_ORDER, R.string.restore_default_str);
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        if (canRestorePreview) {
            menu.setGroupVisible(SELECT_FILE_GROUP1, false);
            menu.setGroupVisible(SELECT_FILE_GROUP2, true);
        } else {
            menu.setGroupVisible(SELECT_FILE_GROUP1, true);
            menu.setGroupVisible(SELECT_FILE_GROUP2, false);
        }
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
            case SELECT_NEW_FILE_ITEM:
                selectPicFromGallery2();
                break;
            case SELECT_FILE_ITEM:
                selectPicFromGallery2();
                break;
            case RESTORE_DEFAULT_PREVIEW:
                resotreBackgroundByDefault();
                break;
            case RESET:
                restoreDefaultHSCI();
                break;
            default:
                return true;
        }
        return false;
    }

    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        setNewBtnHighlight();
        int id = seekBar.getId();
        switch (id) {
            case R.id.hcontrol:
                mHueValue = progress;
                //The screen hue display value varies from -180 to 180.
                mHTv.setText(getString(R.string.hue_str, progress - 180));
                break;
            case R.id.scontrol:
                mSaturationValue = progress;
                //Change the saturation display value varies from (-180, 180) to (-1.0, 1.0).
                mSTv.setText(getString(R.string.saturation_str,
                        ((int) (((progress - 180) * 100) / 180.0)) / 100.0));
                break;
            case R.id.ccontrol:
                mContrastValue = progress;
                //Change the contrast display value varies from (-180, 180) to (-1.0, 1.0).
                mCTv.setText(getString(R.string.contrast_str,
                        ((int) (((progress - 180) * 100) / 180.0)) / 100.0));
                break;
            case R.id.icontrol:
                mIntensityValue = progress;
                //The screen intensity display value varies from -200 to 200.
                mITv.setText(getString(R.string.intensity_str, progress - 200));
                break;
            default:
                break;

        }
        Log.i(TAG, "onProgressChanged mHueValue=" + mHueValue + " mSaturationValue="
                + mSaturationValue + " mNewContrastValue=" + mContrastValue + " mIntensityValue="
                + mIntensityValue);
        try {
            if (null != mPPService && mPPService.getPPStatus()) {
                mPPService.updateHSIC(mHueValue, mSaturationValue, mIntensityValue,
                                     mContrastValue);
            }
        } catch (RemoteException e) {
            Log.e(TAG, "updateHSIC exception");
        }
    }

    public void onStartTrackingTouch(SeekBar seekBar) {
        setActivated(seekBar, true);
    }

    public void onStopTrackingTouch(SeekBar seekBar) {
        setActivated(seekBar, false);
    }

    private void setActivated(SeekBar seekBar, boolean isActivated) {
        int id = seekBar.getId();
        switch (id) {
            case R.id.hcontrol:
                mReduceH.setActivated(isActivated);
                mIncreaseH.setActivated(isActivated);
                break;
            case R.id.scontrol:
                mReduceS.setActivated(isActivated);
                mIncreaseS.setActivated(isActivated);
                break;
            case R.id.icontrol:
                mReduceI.setActivated(isActivated);
                mIncreaseI.setActivated(isActivated);
                break;
            case R.id.ccontrol:
                mReduceC.setActivated(isActivated);
                mIncreaseC.setActivated(isActivated);
                break;
            default:
                break;
        }

    }

    public void onClick(View v) {
        int id = v.getId();

        switch (id) {
            case R.id.cancel:
                restoreSavedHSCI(false);
                finish();
                break;
            case R.id.save:
                saveHSCI();
                break;
            case R.id.previous_btn:
                previousOrNewHSCI(true);
                break;
            case R.id.new_btn:
                previousOrNewHSCI(false);
                break;
            case R.id.up_down:
                upDownHSCISettingLayout();
                break;
            case R.id.more:
                showMoreMenus();
                break;
            case R.id.reduce_hue:
                mHBar.setProgress((mHueValue - 1 > mMinHueValue) ? (mHueValue - 1) : mMinHueValue);
                break;
            case R.id.increase_hue:
                mHBar.setProgress((mHueValue + 1 < mMaxHueValue) ? (mHueValue + 1) : mMaxHueValue);
                break;
            case R.id.reduce_saturation:
                mSBar.setProgress((mSaturationValue - 1 > mMinSaturationValue) ?
                    (mSaturationValue - 1) : mMinSaturationValue);
                break;
            case R.id.increase_saturation:
                mSBar.setProgress((mSaturationValue + 1 < mMaxSaturationValue) ?
                    (mSaturationValue + 1) : mMaxSaturationValue);
                break;
            case R.id.reduce_intensity:
                mIBar.setProgress((mIntensityValue - 1 > mMinIntensityValue) ?
                    (mIntensityValue - 1) : mMinIntensityValue);
                break;
            case R.id.increase_intensity:
                mIBar.setProgress((mIntensityValue + 1 < mMaxIntensityValue) ?
                    (mIntensityValue + 1) : mMaxIntensityValue);
                break;
            case R.id.reduce_contrast:
                mCBar.setProgress((mContrastValue - 1 > mMinContrastValue) ?
                    (mContrastValue - 1) : mMinContrastValue);
                break;
            case R.id.increase_contrast:
                mCBar.setProgress((mContrastValue + 1 < mMaxContrastValue) ?
                    (mContrastValue + 1) : mMaxContrastValue);
                break;
            default:
                break;
        }
    }

    private void selectPicFromGallery2() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        startActivityForResult(intent, REQUEST_SELECT_FILE);
    }

    private void restoreDefaultHSCI() {
        mHBar.setProgress(mDefaultHueValue);
        mSBar.setProgress(mDefaultSaturationValue);
        mIBar.setProgress(mDefaultIntensityValue);
        mCBar.setProgress(mDefaultContrastValue);
        setNewBtnHighlight();
    }

    private void restoreSavedHSCI(boolean setProgress) {
        SharedPreferences share = getSharedPreferences(PREVIEW_STRING_NAME,
                Context.MODE_WORLD_WRITEABLE);
        int hueValue = share.getInt(COLOR_HUE, mDefaultHueValue);
        int saturationValue = share.getInt(COLOR_SATURATION, mDefaultSaturationValue);
        int intensityValue = share.getInt(COLOR_INTENSITY, mDefaultIntensityValue);
        int contrastValue = share.getInt(COLOR_CONTRAST, mDefaultContrastValue);
        if (setProgress) {
            mHBar.setProgress(hueValue);
            mSBar.setProgress(saturationValue);
            mIBar.setProgress(intensityValue);
            mCBar.setProgress(contrastValue);
        }
        Log.i(TAG, "restoreSavedHSCI hueValue=" + hueValue + " saturationValue=" + saturationValue
                + " contrastValue=" + contrastValue + " intensityValue=" + intensityValue);
        try {
            if (null != mPPService && mPPService.getPPStatus()) {
                mPPService.updateHSIC(hueValue, saturationValue, intensityValue, contrastValue);
            }
        } catch (RemoteException e) {
            Log.e(TAG, "updateHSIC exception");
        }
    }

    private void previewNewHSIC() {
        mHBar.setProgress(mHueValue);
        mSBar.setProgress(mSaturationValue);
        mIBar.setProgress(mIntensityValue);
        mCBar.setProgress(mContrastValue);
        Log.i(TAG, "previewNewHSIC mHueValue=" + mHueValue + " mSaturationValue="
                + mSaturationValue + " mContrastValue=" + mContrastValue + " mIntensityValue="
                + mIntensityValue);
        try {
            if (null != mPPService && mPPService.getPPStatus()) {
                mPPService.updateHSIC(mHueValue, mSaturationValue, mIntensityValue,
                        mContrastValue);
            }
        } catch (RemoteException e) {
            Log.e(TAG, "updateHSIC exception");
        }
    }

    private void setNewBtnHighlight() {
        mPreviousBtn.setBackgroundResource(R.drawable.ic_previous_default);
        mNewBtn.setBackgroundResource(R.drawable.ic_new_glow);
        mPreviousBtn.setEnabled(true);
        mNewBtn.setEnabled(true);
        mSaveBtn.setEnabled(true);

    }

    private void initBtnsStatus() {
        mPreviousBtn.setBackgroundResource(R.drawable.ic_previous_dis);
        mNewBtn.setBackgroundResource(R.drawable.ic_new_dis);
        mPreviousBtn.setEnabled(false);
        mNewBtn.setEnabled(false);
        mSaveBtn.setEnabled(false);
    }

    private void previousOrNewHSCI(boolean isPrevious) {
        if (View.GONE != mScreenColorLayout.getVisibility()) {
            mScreenColorLayout.setVisibility(View.GONE);
            mUpdown.setBackgroundResource(R.drawable.up_button);
        }
        if (isPrevious) {
            mPreviousBtn.setBackgroundResource(R.drawable.ic_previous_glow);
            mNewBtn.setBackgroundResource(R.drawable.ic_new_default);
            mUpdown.setVisibility(View.GONE);
            mMore.setVisibility(View.GONE);
            restoreSavedHSCI(false);
        } else {
            mPreviousBtn.setBackgroundResource(R.drawable.ic_previous_default);
            mNewBtn.setBackgroundResource(R.drawable.ic_new_glow);
            mUpdown.setVisibility(View.VISIBLE);
            mMore.setVisibility(View.VISIBLE);
            previewNewHSIC();
        }
    }

    private void saveHSCI() {
        // TODO save HSCI values.
        saveHSICPreference(mHueValue, mSaturationValue, mIntensityValue, mContrastValue);
        finish();
    }

    private void changePreviewByData(Intent data) {
        if (data != null) {
            Uri uri = data.getData();
            setBackgroundByUri(uri);
            savePreviewKey(uri.toString());
        }
    }

    private void setBackgroundByUri(Uri uri) {
        InputStream is = null;
        try {
            is = getContentResolver().openInputStream(uri);
            BitmapFactory.Options opts = new BitmapFactory.Options();
            opts.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(is, null, opts);
            is.close();
            is = getContentResolver().openInputStream(uri);
            opts.inJustDecodeBounds = false;
            opts.inSampleSize = calculateInSampleSize(opts,
                    mRLayout.getWidth(), mRLayout.getHeight());
            Bitmap bm = BitmapFactory.decodeStream(is, null, opts);
            BitmapDrawable bd = new BitmapDrawable(bm);
            mRLayout.setBackgroundDrawable(bd);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        canRestorePreview = true;
    }

    public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }

    private void resotreBackgroundByDefault() {
        mRLayout.setBackgroundResource(R.drawable.default_screencolor_setting);
        savePreviewKey("");
        canRestorePreview = false;
    }

    // use sharepreference to save preview image.
    private void savePreviewKey(String value) {
        SharedPreferences share = getSharedPreferences(PREVIEW_STRING_NAME, Context.MODE_PRIVATE);
        Editor editor = share.edit();
        editor.putString(PREVIEW_STRING_KEY, value);
        editor.commit();
    }

    private void saveHSICPreference(int h, int s, int i, int c) {
        SharedPreferences share = getSharedPreferences(PREVIEW_STRING_NAME, Context.MODE_PRIVATE);
        Editor editor = share.edit();
        editor.putInt(COLOR_HUE, h);
        editor.putInt(COLOR_SATURATION, s);
        editor.putInt(COLOR_INTENSITY, i);
        editor.putInt(COLOR_CONTRAST, c);
        editor.commit();
    }

    private String getScreenColorPreviewKey() {
        SharedPreferences share = getSharedPreferences(PREVIEW_STRING_NAME,
                Context.MODE_WORLD_WRITEABLE | Context.MODE_WORLD_READABLE);
        return share.getString(PREVIEW_STRING_KEY, "");
    }

    private void upDownHSCISettingLayout() {
        if (View.GONE != mScreenColorLayout.getVisibility()) {
            mScreenColorLayout.setVisibility(View.GONE);
            mUpdown.setBackgroundResource(R.drawable.up_button);
        } else {
            mScreenColorLayout.setVisibility(View.VISIBLE);
            mUpdown.setBackgroundResource(R.drawable.down_button);
        }
    }

    private void showMoreMenus() {
        PopupMenu popup = new PopupMenu(CustomScreenColor.this, mMore);
        Menu menu = popup.getMenu();
        popup.getMenuInflater().inflate(R.menu.screencolor_more, menu);

        if (canRestorePreview) {
            menu.removeItem(R.id.selectfile_menu);
        } else {
            menu.removeItem(R.id.restore_preview);
            menu.removeItem(R.id.selectnewfile_menu);
        }

        popup.setOnMenuItemClickListener(new MyMenuItemclick());
        popup.show();
    }

    private class MyMenuItemclick implements OnMenuItemClickListener {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.restore_preview:
                    resotreBackgroundByDefault();
                    return true;
                case R.id.selectnewfile_menu:
                    selectPicFromGallery2();
                    return true;
                case R.id.selectfile_menu:
                    selectPicFromGallery2();
                    return true;
                case R.id.restore_default_str:
                    restoreDefaultHSCI();
                    return true;
            }
            return true;
        }
    }
}