1 /*
2  * Copyright 2012 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 package com.skia;
9 
10 import android.app.ActionBar;
11 import android.app.Activity;
12 import android.app.DownloadManager;
13 import android.content.Intent;
14 import android.content.Context;
15 import android.os.Build;
16 import android.os.Bundle;
17 import android.os.Handler;
18 import android.os.Message;
19 import android.view.KeyEvent;
20 import android.view.Menu;
21 import android.view.MenuItem;
22 import android.view.ViewGroup;
23 import android.widget.ArrayAdapter;
24 import android.widget.LinearLayout;
25 import android.widget.TextView;
26 import android.widget.Toast;
27 
28 import java.io.File;
29 
30 public class SkiaSampleActivity extends Activity
31 {
32     private TextView mTitle;
33     private SkiaSampleView mSampleView;
34 
35     private ArrayAdapter<String> mSlideList;
36 
37     @Override
onCreate(Bundle savedInstanceState)38     public void onCreate(Bundle savedInstanceState) {
39         super.onCreate(savedInstanceState);
40 
41         setContentView(R.layout.layout);
42         mTitle = (TextView) findViewById(R.id.title_view);
43         mSlideList = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1);
44 
45         try {
46             System.loadLibrary("skia_android");
47         } catch (UnsatisfiedLinkError e) {
48             // This might be because skia was linked to SampleApp statically.
49         }
50 
51         try {
52             System.loadLibrary("SampleApp");
53 
54             createSampleView(false, 0);
55 
56             setupActionBar();
57         } catch (UnsatisfiedLinkError e) {
58             mTitle.setText("ERROR: native library could not be loaded");
59         }
60     }
61 
createSampleView(boolean useOpenGLAPI, int msaaSampleCount)62     private void createSampleView(boolean useOpenGLAPI, int msaaSampleCount) {
63         if (mSampleView != null) {
64             ViewGroup viewGroup = (ViewGroup) mSampleView.getParent();
65             viewGroup.removeView(mSampleView);
66             mSampleView.terminate();
67         }
68 
69         // intent get intent extras if triggered from the command line
70         Intent intent = this.getIntent();
71         String flags = intent.getStringExtra("cmdLineFlags");
72 
73         if (flags == null || flags.isEmpty()) {
74             flags  = "--pictureDir /data/local/tmp/skia_skp ";
75             flags += "--resourcePath /data/local/tmp/skia_resources ";
76         }
77 
78         mSampleView = new SkiaSampleView(this, flags, useOpenGLAPI, msaaSampleCount);
79         LinearLayout holder = (LinearLayout) findViewById(R.id.holder);
80         holder.addView(mSampleView, new LinearLayout.LayoutParams(
81                     ViewGroup.LayoutParams.MATCH_PARENT,
82                     ViewGroup.LayoutParams.MATCH_PARENT));
83     }
84 
setupActionBar()85     private void setupActionBar() {
86         ActionBar.OnNavigationListener navigationCallback = new ActionBar.OnNavigationListener() {
87             @Override
88             public boolean onNavigationItemSelected(int position, long itemId) {
89                 mSampleView.goToSample(position);
90                 return true;
91             }
92         };
93 
94         ActionBar actionBar = getActionBar();
95         actionBar.setDisplayShowHomeEnabled(false);
96         actionBar.setDisplayShowTitleEnabled(false);
97         actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
98         actionBar.setListNavigationCallbacks(mSlideList, navigationCallback);
99     }
100 
101     @Override
onResume()102     protected void onResume () {
103         super.onResume();
104         if (mSampleView != null && mSampleView.getWidth() > 0 && mSampleView.getHeight() > 0) {
105             //TODO try mSampleView.requestRender() instead
106             mSampleView.inval();
107         }
108     }
109 
110     @Override
onDestroy()111     public void onDestroy() {
112         if (mSampleView != null) {
113             mSampleView.terminate();
114         }
115         super.onDestroy();
116     }
117 
118     @Override
onCreateOptionsMenu(Menu menu)119     public boolean onCreateOptionsMenu(Menu menu) {
120         getMenuInflater().inflate(R.menu.action_bar, menu);
121         return true;
122     }
123 
124     @Override
onPrepareOptionsMenu(Menu menu)125     public boolean onPrepareOptionsMenu(Menu menu) {
126         if (mSampleView != null) {
127             if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
128                 ((MenuItem) menu.findItem(R.id.glcontext_menu))
129                     .setEnabled(false);
130 
131             } else {
132                 boolean usesOpenGLAPI = mSampleView.getUsesOpenGLAPI();
133                 boolean isMSAA4 = mSampleView.getMSAASampleCount() == 4;
134 
135                 ((MenuItem) menu.findItem(R.id.glcontext_opengles))
136                     .setChecked(!usesOpenGLAPI && !isMSAA4);
137 
138                 ((MenuItem) menu.findItem(R.id.glcontext_msaa4_opengles))
139                     .setChecked(!usesOpenGLAPI && isMSAA4);
140 
141                 ((MenuItem) menu.findItem(R.id.glcontext_opengl))
142                     .setChecked(usesOpenGLAPI && !isMSAA4);
143 
144                 ((MenuItem) menu.findItem(R.id.glcontext_msaa4_opengl))
145                     .setChecked(usesOpenGLAPI && isMSAA4);
146             }
147         }
148         return true;
149     }
150 
151     @Override
onOptionsItemSelected(MenuItem item)152     public boolean onOptionsItemSelected(MenuItem item) {
153         if (mSampleView == null) {
154             return false;
155         }
156 
157         switch (item.getItemId()) {
158         case R.id.overview:
159             mSampleView.showOverview();
160             return true;
161         case R.id.prev:
162             mSampleView.previousSample();
163             return true;
164         case R.id.next:
165             mSampleView.nextSample();
166             return true;
167         case R.id.toggle_rendering:
168             mSampleView.toggleRenderingMode();
169             return true;
170         case R.id.slideshow:
171             mSampleView.toggleSlideshow();
172             return true;
173         case R.id.fps:
174             mSampleView.toggleFPS();
175             return true;
176         case R.id.tiling:
177             mSampleView.toggleTiling();
178             return true;
179         case R.id.bbox:
180             mSampleView.toggleBBox();
181             return true;
182         case R.id.save_to_pdf:
183             mSampleView.saveToPDF();
184             return true;
185         case R.id.glcontext_opengles:
186             return setOpenGLContextSettings(false, 0);
187         case R.id.glcontext_msaa4_opengles:
188             return setOpenGLContextSettings(false, 4);
189         case R.id.glcontext_opengl:
190             return setOpenGLContextSettings(true, 0);
191         case R.id.glcontext_msaa4_opengl:
192             return setOpenGLContextSettings(true, 4);
193         default:
194             return false;
195         }
196     }
197 
198     @Override
dispatchKeyEvent(KeyEvent event)199     public boolean dispatchKeyEvent(KeyEvent event) {
200         final int keycode = event.getKeyCode();
201         if (keycode == KeyEvent.KEYCODE_BACK) {
202             if (event.getAction() == KeyEvent.ACTION_UP) {
203                 finish();
204             }
205             return true;
206         }
207         return false;
208     }
209 
210     private static final int SET_TITLE = 1;
211     private static final int SET_SLIDES = 2;
212     private static final int TOAST_DOWNLOAD = 3;
213 
214     private Handler mHandler = new Handler() {
215         @Override
216         public void handleMessage(Message msg) {
217             switch (msg.what) {
218             case SET_TITLE:
219                 mTitle.setText((String) msg.obj);
220                 SkiaSampleActivity.this.getActionBar().setSubtitle((String) msg.obj);
221                 break;
222             case SET_SLIDES:
223                 mSlideList.addAll((String[]) msg.obj);
224                 break;
225             case TOAST_DOWNLOAD:
226                 Toast.makeText(SkiaSampleActivity.this, (String) msg.obj,
227                         Toast.LENGTH_SHORT).show();
228                 break;
229             default:
230                 break;
231             }
232         }
233     };
234 
235     // Called by JNI
236     @Override
setTitle(CharSequence title)237     public void setTitle(CharSequence title) {
238         mHandler.obtainMessage(SET_TITLE, title).sendToTarget();
239     }
240 
241     // Called by JNI
setSlideList(String[] slideList)242     public void setSlideList(String[] slideList) {
243         mHandler.obtainMessage(SET_SLIDES, slideList).sendToTarget();
244     }
245 
246     // Called by JNI
addToDownloads(final String title, final String desc, final String path)247     public void addToDownloads(final String title, final String desc, final String path) {
248         File file = new File(path);
249         final long length = file.exists() ? file.length() : 0;
250         if (length == 0) {
251             String failed = getString(R.string.save_failed);
252             mHandler.obtainMessage(TOAST_DOWNLOAD, failed).sendToTarget();
253             return;
254         }
255         String toast = getString(R.string.file_saved).replace("%s", title);
256         mHandler.obtainMessage(TOAST_DOWNLOAD, toast).sendToTarget();
257         final DownloadManager manager =
258                 (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
259         new Thread("Add PDF to downloads") {
260             @Override
261             public void run() {
262                 final String mimeType = "application/pdf";
263                 manager.addCompletedDownload(title, desc, true, mimeType, path, length, true);
264             }
265         }.start();
266     }
267 
setOpenGLContextSettings(boolean requestedOpenGLAPI, int requestedSampleCount)268     private boolean setOpenGLContextSettings(boolean requestedOpenGLAPI, int requestedSampleCount) {
269         if (mSampleView != null &&
270                 mSampleView.getMSAASampleCount() == requestedSampleCount &&
271                 mSampleView.getUsesOpenGLAPI() == requestedOpenGLAPI) {
272             return true;
273         }
274 
275         createSampleView(requestedOpenGLAPI, requestedSampleCount);
276 
277         return true;
278     }
279 }
280