• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.deviceconfig;
18 
19 import android.app.Activity;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.res.Configuration;
23 import android.net.Uri;
24 import android.opengl.GLSurfaceView;
25 import android.os.Bundle;
26 import android.util.DisplayMetrics;
27 import android.view.View;
28 import android.view.View.OnClickListener;
29 import android.view.ViewGroup;
30 import android.widget.Button;
31 import android.widget.LinearLayout;
32 import android.widget.TextView;
33 
34 import java.io.File;
35 
36 import javax.microedition.khronos.egl.EGLConfig;
37 import javax.microedition.khronos.opengles.GL10;
38 
39 public class MyActivity extends Activity implements OnClickListener {
40 
41     public static final String TAG = "DeviceConfig";
42     private static GLView mGl;
43 
44     /** Called when the activity is first created. */
45     @Override
onCreate(Bundle savedInstanceState)46     public void onCreate(Bundle savedInstanceState) {
47         super.onCreate(savedInstanceState);
48 
49         setContentView(R.layout.main);
50         // Instantiate a GL surface view so we can get extensions information
51         mGl = new GLView(this);
52         LinearLayout vg = (LinearLayout) findViewById(R.id.buttonHolder);
53         // If we set the layout to be 0, it just won't render
54         ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(1, 1);
55         mGl.setLayoutParams(params);
56         vg.addView(mGl);
57 
58         Button btn = (Button) findViewById(R.id.generateConfigButton);
59         btn.setOnClickListener(this);
60         Configuration config = getResources().getConfiguration();
61 
62         TextView tv = (TextView) findViewById(R.id.keyboard_state_api);
63         if (tv != null) {
64             String separator = config.orientation == Configuration.ORIENTATION_PORTRAIT ? "\n" : "";
65             String foo = "keyboardHidden=" + separator;
66             if (config.keyboardHidden == Configuration.KEYBOARDHIDDEN_NO) {
67                 foo += "EXPOSED";
68             } else if (config.keyboardHidden == Configuration.KEYBOARDHIDDEN_YES) {
69                 foo += "HIDDEN";
70             } else if (config.keyboardHidden == Configuration.KEYBOARDHIDDEN_UNDEFINED) {
71                 foo += "UNDEFINED";
72             } else {
73                 foo += "?";
74             }
75             foo += "\nhardKeyboardHidden=" + separator;
76             if (config.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
77                 foo = foo + "EXPOSED";
78             } else if (config.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
79                 foo = foo + "HIDDEN";
80             } else if (config.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_UNDEFINED) {
81                 foo = foo + "UNDEFINED";
82             } else {
83                 foo = "?";
84             }
85 
86             tv.setText(foo);
87         }
88 
89         tv = (TextView) findViewById(R.id.nav_state_api);
90         if (tv != null) {
91             if (config.navigationHidden == Configuration.NAVIGATIONHIDDEN_NO) {
92                 tv.setText("EXPOSED");
93             } else if (config.navigationHidden == Configuration.NAVIGATIONHIDDEN_YES) {
94                 tv.setText("HIDDEN");
95             } else if (config.navigationHidden == Configuration.NAVIGATIONHIDDEN_UNDEFINED) {
96                 tv.setText("UNDEFINED");
97             } else {
98                 tv.setText("??");
99             }
100         }
101 
102         DisplayMetrics metrics = getResources().getDisplayMetrics();
103 
104 
105         tv = (TextView) findViewById(R.id.size_api);
106         if (tv != null) {
107             int a = metrics.heightPixels;
108             int b = metrics.widthPixels;
109             tv.setText(b + "x" + a);
110         }
111 
112         tv = (TextView) findViewById(R.id.xdpi);
113         if (tv != null) {
114             tv.setText(String.format("%f", metrics.xdpi));
115         }
116         tv = (TextView) findViewById(R.id.ydpi);
117         if (tv != null) {
118             tv.setText(String.format("%f", metrics.ydpi));
119         }
120 
121         tv = (TextView) findViewById(R.id.scaled_density);
122         if (tv != null) {
123             tv.setText(String.format("%f", metrics.scaledDensity));
124         }
125 
126         tv = (TextView) findViewById(R.id.font_scale);
127         if (tv != null) {
128             tv.setText(String.format("%f", config.fontScale));
129         }
130 
131     }
132 
onClick(View v)133     public void onClick(View v) {
134         ConfigGenerator configGen = new ConfigGenerator(this, mGl.getExtensions());
135         final String filename = configGen.generateConfig();
136         if (filename != null) {
137             Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
138             emailIntent.setType("text/xml");
139             File devicesXml = new File(filename);
140             emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Device XML: " + devicesXml.getName());
141             emailIntent.putExtra(Intent.EXTRA_TEXT, "Note: This is intended to generate a base "
142                     + "XML description. After running this, you should double check the generated "
143                     + "information and add all of the missing fields.");
144             emailIntent.putExtra(Intent.EXTRA_STREAM,
145                     Uri.parse("file://" + devicesXml.getAbsolutePath()));
146             startActivity(emailIntent);
147         }
148     }
149 
150     private static class GLView extends GLSurfaceView {
151         private GlRenderer mRenderer;
152 
GLView(Context context)153         public GLView(Context context) {
154             super(context);
155             setEGLContextClientVersion(2);
156             mRenderer = new GlRenderer();
157             setRenderer(mRenderer);
158             requestRender();
159         }
160 
getExtensions()161         public String getExtensions() {
162             return mRenderer.extensions;
163         }
164 
165     }
166 
167     private static class GlRenderer implements GLSurfaceView.Renderer {
168         public String extensions = "";
169 
onDrawFrame(GL10 gl)170         public void onDrawFrame(GL10 gl) {
171         }
172 
onSurfaceChanged(GL10 gl, int width, int height)173         public void onSurfaceChanged(GL10 gl, int width, int height) {
174             gl.glViewport(0, 0, 0, 0);
175         }
176 
onSurfaceCreated(GL10 gl, EGLConfig config)177         public void onSurfaceCreated(GL10 gl, EGLConfig config) {
178             if (extensions.equals("")) {
179                 String extensions10 = gl.glGetString(GL10.GL_EXTENSIONS);
180                 if(extensions10 != null) {
181                     extensions += extensions10;
182                 }
183             }
184         }
185     }
186 }
187