OSDN Git Service

livedisplay: Add picture adjustment settings
[android-x86/packages-apps-Settings.git] / src / com / android / settings / ScreenColorSettings.java
1 /*
2    Copyright (c) 2014, The Linux Foundation. All Rights Reserved.
3 Redistribution and use in source and binary forms, with or without
4 modification, are permitted provided that the following conditions are
5 met:
6     * Redistributions of source code must retain the above copyright
7       notice, this list of conditions and the following disclaimer.
8     * Redistributions in binary form must reproduce the above
9       copyright notice, this list of conditions and the following
10       disclaimer in the documentation and/or other materials provided
11       with the distribution.
12     * Neither the name of The Linux Foundation nor the names of its
13       contributors may be used to endorse or promote products derived
14       from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 package com.android.settings;
30
31 import java.io.FileNotFoundException;
32 import java.io.InputStream;
33
34 import android.app.ActionBar;
35 import android.app.Activity;
36 import android.content.ComponentName;
37 import android.content.Context;
38 import android.content.Intent;
39 import android.content.ServiceConnection;
40 import android.content.SharedPreferences;
41 import android.content.SharedPreferences.Editor;
42 import android.graphics.Bitmap;
43 import android.graphics.BitmapFactory;
44 import android.graphics.drawable.BitmapDrawable;
45 import android.net.Uri;
46 import android.os.Bundle;
47 import android.os.IBinder;
48 import android.os.RemoteException;
49 import android.util.Log;
50 import android.view.Gravity;
51 import android.view.LayoutInflater;
52 import android.view.Menu;
53 import android.view.MenuItem;
54 import android.view.View;
55 import android.view.View.OnClickListener;
56 import android.view.ViewGroup;
57 import android.view.Window;
58 import android.widget.AdapterView;
59 import android.widget.AdapterView.OnItemClickListener;
60 import android.widget.ArrayAdapter;
61 import android.widget.ListView;
62 import android.widget.Button;
63 import android.widget.ImageView;
64 import android.widget.LinearLayout;
65 import android.widget.PopupMenu;
66 import android.widget.RadioButton;
67 import android.widget.PopupMenu.OnMenuItemClickListener;
68 import android.widget.RadioGroup;
69 import android.widget.RelativeLayout;
70 import android.widget.SeekBar;
71 import android.widget.TextView;
72 import android.widget.PopupWindow;
73
74 import com.android.display.IPPService;
75
76 public class ScreenColorSettings extends Activity {
77     private final static String TAG = "ScreenColorSettings";
78     private final static boolean DEBUG = false;
79
80     private final static int MODE_STANDARD = 0;
81     private final static int MODE_VIVID    = 1;
82     private final static int MODE_CUSTOM   = 2;
83
84     private static final String COLOR_MODE       = "mode";
85     private static final String COLOR_HUE        = "hue";
86     private static final String COLOR_SATURATION = "saturation";
87     private static final String COLOR_INTENSITY  = "intensity";
88     private static final String COLOR_CONTRAST   = "contrast";
89
90     private static final String PREVIEW_STRING_NAME = "screencolor_preview_name";
91
92     private int mMode = 0;
93     private IPPService mPPService = null;
94     private PPServiceConnection mPPServiceConn = null;
95     private SharedPreferences mSharedPreference;
96
97     @Override
98     protected void onCreate(Bundle savedInstanceState) {
99         super.onCreate(savedInstanceState);
100         initPPService();
101         getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
102         setContentView(R.layout.screencolor_settings);
103
104         ListView listView = (ListView)findViewById(R.id.mode_list);
105         listView.setBackgroundColor(R.color.screencolor_background);
106         mSharedPreference = getSharedPreferences(PREVIEW_STRING_NAME, Context.MODE_PRIVATE);
107         mMode = mSharedPreference.getInt(COLOR_MODE, 0);
108         setScreenColorMode(mMode);
109         String[] list = getResources().getStringArray(R.array.screen_color_setting);
110         ColorModeAdaper adapter = new ColorModeAdaper(this,
111                 R.layout.screen_color_item, list);
112         listView.setAdapter(adapter);
113     }
114
115     private class ColorModeAdaper extends ArrayAdapter<String> {
116         private int mResource;
117         private RelativeLayout mItemView;
118
119         public ColorModeAdaper(Context context, int resource, String[] list) {
120             super(context, resource, list);
121             this.mResource = resource;
122         }
123
124         @Override
125         public View getView(int position, View convertView, ViewGroup parent) {
126             String name = getItem(position);
127             if(convertView == null) {
128                 mItemView = new RelativeLayout(getContext());
129                 LayoutInflater vi = (LayoutInflater)getContext()
130                         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
131                 vi.inflate(mResource, mItemView, true);
132             } else {
133                 mItemView = (RelativeLayout) convertView;
134             }
135             ((TextView) mItemView.findViewById(android.R.id.title)).setText(name);
136
137             RadioButton radioButton = (RadioButton) mItemView.findViewById(android.R.id.button1);
138             ImageView setting = (ImageView) mItemView.findViewById(android.R.id.button2);
139             if (mMode == position) {
140                 radioButton.setChecked(true);
141                 setting.setEnabled(true);
142             } else {
143                 radioButton.setChecked(false);
144                 setting.setEnabled(false);
145             }
146
147             if (position != getCount()-1) {
148                 ((ImageView) mItemView.findViewById(R.id.divider)).setVisibility(View.INVISIBLE);
149                 setting.setVisibility(View.INVISIBLE);
150             } else {
151                 setting.setOnClickListener(new OnClickListener () {
152                     @Override
153                     public void onClick(View v) {
154                         Intent intent = new Intent();
155                         intent.setClass(ScreenColorSettings.this, CustomScreenColor.class);
156                         startActivity(intent);
157                     }});
158             }
159
160             View v = mItemView.findViewById(R.id.widget_frame);
161             v.setTag(position);
162             v.setOnClickListener(new OnClickListener () {
163                 @Override
164                 public void onClick(View v) {
165                     int mode = (Integer)(v.getTag());
166                     setScreenColorMode(mode);
167                     notifyDataSetChanged();
168                 }});
169             return mItemView;
170         }
171     }
172
173     private void initPPService() {
174         mPPServiceConn = new PPServiceConnection();
175         String packetname = "com.qualcomm.display";
176         String service = "com.qualcomm.display.PPService";
177         Intent i = new Intent(IPPService.class.getName());
178
179         ComponentName component = new ComponentName(packetname, service);
180         i.setComponent(component);
181
182         bindService(i, mPPServiceConn, Context.BIND_AUTO_CREATE);
183     }
184
185     private class PPServiceConnection implements ServiceConnection {
186
187         @Override
188         public void onServiceConnected(ComponentName name, IBinder service) {
189             mPPService = IPPService.Stub.asInterface((IBinder) service);
190             if (DEBUG) Log.d(TAG, "onServiceConnected, service=" + mPPService);
191             try {
192                 if (null != mPPService) {
193                     mPPService.startPP();
194                     setScreenColorMode(mMode);
195                 }
196             } catch (RemoteException e) {
197                 Log.e(TAG, "startPP exception");
198             }
199         }
200
201         @Override
202         public void onServiceDisconnected(ComponentName name) {
203             if (null != mPPService) {
204                 try {
205                     mPPService.stopPP();
206                 } catch (RemoteException e) {
207                     Log.e(TAG, "stopPP exception");
208                 }
209             }
210             mPPService = null;
211         }
212
213     }
214
215     private void setScreenColorMode(int mode) {
216         if (DEBUG) Log.i(TAG, "setScreenColorMode mode = " + mode);
217         int hue = 0;
218         int saturation = 0;
219         int intensity = 0;
220         int contrast = 0;
221         switch (mode) {
222             case MODE_STANDARD:
223                 hue = 180;
224                 saturation = 180;
225                 intensity = 255;
226                 contrast = 180;
227                 break;
228             case MODE_VIVID:
229                 hue = 180;
230                 saturation = 210;
231                 intensity = 255;
232                 contrast = 180;
233                 break;
234             case MODE_CUSTOM:
235                 hue = mSharedPreference.getInt(COLOR_HUE, 180);
236                 saturation = mSharedPreference.getInt(COLOR_SATURATION, 180);
237                 intensity = mSharedPreference.getInt(COLOR_INTENSITY, 255);
238                 contrast = mSharedPreference.getInt(COLOR_CONTRAST, 180);
239                 break;
240             default:
241                 break;
242         }
243         saveModeValue(mode);
244         updateHSCIValue(hue, saturation, intensity, contrast);
245     }
246
247     private void updateHSCIValue(int hue, int saturation, int intensity, int contrast) {
248         try {
249             if (null != mPPService && mPPService.getPPStatus()) {
250                 mPPService.updateHSIC(hue, saturation, intensity, contrast);
251             }
252         } catch (RemoteException e) {
253             Log.e(TAG, "updateHSIC exception");
254         }
255     }
256
257     @Override
258     protected void onDestroy() {
259         super.onDestroy();
260         unbindService(mPPServiceConn);
261     }
262
263     private void saveModeValue(int mode) {
264         mMode = mode;
265         Editor editor = mSharedPreference.edit();
266         editor.putInt(COLOR_MODE, mode);
267         editor.commit();
268     }
269 }