Debian Codegraph @

apps/DeviceConfig/app/src/main/java/com/example/android/deviceconfig/MyActivity.java

raw
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.annotation.SuppressLint;
20 import android.app.Activity;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.res.Configuration;
24 import android.net.Uri;
25 import android.opengl.GLSurfaceView;
26 import android.os.Bundle;
27 import android.util.DisplayMetrics;
28 import android.view.View;
29 import android.view.View.OnClickListener;
30 import android.view.ViewGroup;
31 import android.view.WindowManager;
32 import android.widget.Button;
33 import android.widget.LinearLayout;
34 import android.widget.TextView;
35
36 import java.io.File;
37
38 import javax.microedition.khronos.egl.EGLConfig;
39 import javax.microedition.khronos.opengles.GL10;
40
41 public class MyActivity extends Activity implements OnClickListener {
42
43 private static GLView mGl;
44
45 /** Called when the activity is first created. */
46 @SuppressLint("SetTextI18n")
47 @Override
48 public void onCreate(Bundle savedInstanceState) {
49 super.onCreate(savedInstanceState);
50
51 setContentView(R.layout.main);
52 LinearLayout vg = (LinearLayout) findViewById(R.id.buttonHolder);
53 if (vg == null) {
54 return;
55 }
56
57 // Instantiate a GL surface view so we can get extensions information
58 mGl = new GLView(this);
59 // If we set the layout to be 0, it just won't render
60 ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(1, 1);
61 mGl.setLayoutParams(params);
62 vg.addView(mGl);
63
64 Button btn = (Button) findViewById(R.id.generateConfigButton);
65 btn.setOnClickListener(this);
66 Configuration config = getResources().getConfiguration();
67
68 TextView tv = (TextView) findViewById(R.id.keyboard_state_api);
69 if (tv != null) {
70 String separator = config.orientation == Configuration.ORIENTATION_PORTRAIT ? "\n" : "";
71 String foo = "keyboardHidden=" + separator;
72 if (config.keyboardHidden == Configuration.KEYBOARDHIDDEN_NO) {
73 foo += "EXPOSED";
74 } else if (config.keyboardHidden == Configuration.KEYBOARDHIDDEN_YES) {
75 foo += "HIDDEN";
76 } else if (config.keyboardHidden == Configuration.KEYBOARDHIDDEN_UNDEFINED) {
77 foo += "UNDEFINED";
78 } else {
79 foo += "?";
80 }
81 foo += "\nhardKeyboardHidden=" + separator;
82 if (config.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
83 foo = foo + "EXPOSED";
84 } else if (config.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
85 foo = foo + "HIDDEN";
86 } else if (config.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_UNDEFINED) {
87 foo = foo + "UNDEFINED";
88 } else {
89 foo = "?";
90 }
91
92 tv.setText(foo);
93 }
94
95 tv = (TextView) findViewById(R.id.nav_state_api);
96 if (tv != null) {
97 if (config.navigationHidden == Configuration.NAVIGATIONHIDDEN_NO) {
98 tv.setText("EXPOSED");
99 } else if (config.navigationHidden == Configuration.NAVIGATIONHIDDEN_YES) {
100 tv.setText("HIDDEN");
101 } else if (config.navigationHidden == Configuration.NAVIGATIONHIDDEN_UNDEFINED) {
102 tv.setText("UNDEFINED");
103 } else {
104 tv.setText("??");
105 }
106 }
107
108 DisplayMetrics metrics = getResources().getDisplayMetrics();
109
110 tv = (TextView) findViewById(R.id.size_api);
111 if (tv != null) {
112 WindowManager windowManager = getWindowManager();
113 int widthPixels = ConfigGenerator.getScreenWidth(windowManager, metrics);
114 int heightPixels = ConfigGenerator.getScreenHeight(windowManager, metrics);
115 tv.setText(widthPixels + "x" + heightPixels);
116 }
117
118 tv = (TextView) findViewById(R.id.xdpi);
119 if (tv != null) {
120 tv.setText(String.format("%f", metrics.xdpi));
121 }
122 tv = (TextView) findViewById(R.id.ydpi);
123 if (tv != null) {
124 tv.setText(String.format("%f", metrics.ydpi));
125 }
126
127 tv = (TextView) findViewById(R.id.scaled_density);
128 if (tv != null) {
129 tv.setText(String.format("%f", metrics.scaledDensity));
130 }
131
132 tv = (TextView) findViewById(R.id.font_scale);
133 if (tv != null) {
134 tv.setText(String.format("%f", config.fontScale));
135 }
136 }
137
138 public void onClick(View v) {
139 ConfigGenerator configGen = new ConfigGenerator(this, mGl.getExtensions());
140 final String filename = configGen.generateConfig();
141 if (filename != null) {
142 Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
143 emailIntent.setType("text/xml");
144 File devicesXml = new File(filename);
145 emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Device XML: " + devicesXml.getName());
146 emailIntent.putExtra(Intent.EXTRA_TEXT, "Note: This is intended to generate a base "
147 + "XML description. After running this, you should double check the generated "
148 + "information and add all of the missing fields.");
149 emailIntent.putExtra(Intent.EXTRA_STREAM,
150 Uri.parse("file://" + devicesXml.getAbsolutePath()));
151 startActivity(emailIntent);
152 }
153 }
154
155 private static class GLView extends GLSurfaceView {
156 private GlRenderer mRenderer;
157
158 public GLView(Context context) {
159 super(context);
160 setEGLContextClientVersion(2);
161 mRenderer = new GlRenderer();
162 setRenderer(mRenderer);
163 requestRender();
164 }
165
166 public String getExtensions() {
167 return mRenderer.extensions;
168 }
169
170 }
171
172 private static class GlRenderer implements GLSurfaceView.Renderer {
173 public String extensions = "";
174
175 public void onDrawFrame(GL10 gl) {
176 }
177
178 public void onSurfaceChanged(GL10 gl, int width, int height) {
179 gl.glViewport(0, 0, 0, 0);
180 }
181
182 public void onSurfaceCreated(GL10 gl, EGLConfig config) {
183 if (extensions.equals("")) {
184 String extensions10 = gl.glGetString(GL10.GL_EXTENSIONS);
185 if(extensions10 != null) {
186 extensions += extensions10;
187 }
188 }
189 }
190 }
191 }