1 /* 2 * Copyright 2015 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 org.skia.viewer; 9 10 import android.app.Activity; 11 import android.content.res.Configuration; 12 import android.os.Bundle; 13 import android.support.v4.widget.DrawerLayout; 14 import android.support.v7.app.ActionBarDrawerToggle; 15 import android.view.KeyEvent; 16 import android.view.Menu; 17 import android.view.MenuInflater; 18 import android.view.MenuItem; 19 import android.view.MotionEvent; 20 import android.view.Surface; 21 import android.view.SurfaceHolder; 22 import android.view.SurfaceView; 23 import android.view.View; 24 import android.widget.ListView; 25 26 public class ViewerActivity 27 extends Activity implements SurfaceHolder.Callback, View.OnTouchListener { 28 private static final float FLING_VELOCITY_THRESHOLD = 1000; 29 30 private DrawerLayout mDrawerLayout; 31 private ActionBarDrawerToggle mDrawerToggle; 32 private ListView mDrawerList; 33 private StateAdapter mStateAdapter; 34 35 private ViewerApplication mApplication; 36 onSurfaceCreated(long handle, Surface surface)37 private native void onSurfaceCreated(long handle, Surface surface); onSurfaceChanged(long handle, Surface surface)38 private native void onSurfaceChanged(long handle, Surface surface); onSurfaceDestroyed(long handle)39 private native void onSurfaceDestroyed(long handle); onKeyPressed(long handle, int keycode)40 private native void onKeyPressed(long handle, int keycode); onTouched(long handle, int owner, int state, float x, float y)41 private native void onTouched(long handle, int owner, int state, float x, float y); onUIStateChanged(long handle, String stateName, String stateValue)42 private native void onUIStateChanged(long handle, String stateName, String stateValue); 43 44 @Override onCreateOptionsMenu(Menu menu)45 public boolean onCreateOptionsMenu(Menu menu) { 46 MenuInflater inflater = getMenuInflater(); 47 inflater.inflate(R.menu.title, menu); 48 return true; 49 } 50 51 @Override onOptionsItemSelected(MenuItem item)52 public boolean onOptionsItemSelected(MenuItem item) { 53 // Pass the event to ActionBarDrawerToggle, if it returns 54 // true, then it has handled the app icon touch event 55 if (mDrawerToggle != null && mDrawerToggle.onOptionsItemSelected(item)) { 56 return true; 57 } 58 59 switch (item.getItemId()) { 60 case R.id.action_left: 61 onKeyPressed(mApplication.getNativeHandle(), KeyEvent.KEYCODE_SOFT_LEFT); 62 return true; 63 case R.id.action_right: 64 onKeyPressed(mApplication.getNativeHandle(), KeyEvent.KEYCODE_SOFT_RIGHT); 65 return true; 66 default: 67 return super.onOptionsItemSelected(item); 68 } 69 } 70 71 @Override onCreate(Bundle savedInstanceState)72 protected void onCreate(Bundle savedInstanceState) { 73 super.onCreate(savedInstanceState); 74 setContentView(R.layout.activity_main); 75 76 SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surfaceView); 77 surfaceView.getHolder().addCallback(this); 78 surfaceView.setOnTouchListener(this); 79 80 mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); 81 if (mDrawerLayout != null) { // xlarge-land has no drawer layout (drawer is always open) 82 mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, 83 R.string.drawer_open, R.string.drawer_close); 84 mDrawerLayout.addDrawerListener(mDrawerToggle); 85 getActionBar().setDisplayHomeAsUpEnabled(true); 86 getActionBar().setHomeButtonEnabled(true); 87 } 88 89 mDrawerList = (ListView) findViewById(R.id.leftDrawer); 90 mStateAdapter = new StateAdapter(this); 91 mDrawerList.setAdapter(mStateAdapter); 92 93 mApplication = (ViewerApplication) getApplication(); 94 mApplication.setViewerActivity(this); 95 } 96 97 @Override onPostCreate(Bundle savedInstanceState)98 protected void onPostCreate(Bundle savedInstanceState) { 99 super.onPostCreate(savedInstanceState); 100 if (mDrawerToggle != null) { 101 mDrawerToggle.syncState(); 102 } 103 } 104 105 @Override onConfigurationChanged(Configuration newConfig)106 public void onConfigurationChanged(Configuration newConfig) { 107 super.onConfigurationChanged(newConfig); 108 if (mDrawerToggle != null) { 109 mDrawerToggle.onConfigurationChanged(newConfig); 110 } 111 } 112 113 @Override onDestroy()114 protected void onDestroy() { 115 mApplication.setViewerActivity(null); 116 super.onDestroy(); 117 } 118 119 @Override surfaceCreated(SurfaceHolder holder)120 public void surfaceCreated(SurfaceHolder holder) { 121 if (mApplication.getNativeHandle() != 0) { 122 onSurfaceCreated(mApplication.getNativeHandle(), holder.getSurface()); 123 } 124 } 125 126 @Override surfaceChanged(SurfaceHolder holder, int format, int width, int height)127 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 128 if (mApplication.getNativeHandle() != 0) { 129 onSurfaceChanged(mApplication.getNativeHandle(), holder.getSurface()); 130 } 131 } 132 133 @Override surfaceDestroyed(SurfaceHolder holder)134 public void surfaceDestroyed(SurfaceHolder holder) { 135 if (mApplication.getNativeHandle() != 0) { 136 onSurfaceDestroyed(mApplication.getNativeHandle()); 137 } 138 } 139 140 @Override onTouch(View v, MotionEvent event)141 public boolean onTouch(View v, MotionEvent event) { 142 int count = event.getPointerCount(); 143 for (int i = 0; i < count; i++) { 144 final float x = event.getX(i); 145 final float y = event.getY(i); 146 final int owner = event.getPointerId(i); 147 int action = event.getAction() & MotionEvent.ACTION_MASK; 148 onTouched(mApplication.getNativeHandle(), owner, action, x, y); 149 } 150 return true; 151 } 152 setState(String stateJson)153 public void setState(String stateJson) { 154 mStateAdapter.setState(stateJson); 155 } 156 onStateChanged(String stateName, String stateValue)157 public void onStateChanged(String stateName, String stateValue) { 158 onUIStateChanged(mApplication.getNativeHandle(), stateName, stateValue); 159 } 160 } 161