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.Application; 11 import android.content.res.AssetManager; 12 13 public class ViewerApplication extends Application { 14 private long mNativeHandle = 0; 15 private ViewerActivity mViewerActivity; 16 private String mStateJsonStr, mTitle; 17 18 static { 19 System.loadLibrary("viewer"); 20 } 21 createNativeApp(AssetManager assetManager)22 private native long createNativeApp(AssetManager assetManager); destroyNativeApp(long handle)23 private native void destroyNativeApp(long handle); 24 25 @Override onCreate()26 public void onCreate() { 27 super.onCreate(); 28 mNativeHandle = createNativeApp(this.getResources().getAssets()); 29 } 30 31 @Override onTerminate()32 public void onTerminate() { 33 if (mNativeHandle != 0) { 34 destroyNativeApp(mNativeHandle); 35 mNativeHandle = 0; 36 } 37 super.onTerminate(); 38 } 39 getNativeHandle()40 public long getNativeHandle() { 41 return mNativeHandle; 42 } 43 setViewerActivity(ViewerActivity viewerActivity)44 public void setViewerActivity(ViewerActivity viewerActivity) { 45 mViewerActivity = viewerActivity; 46 // Note that viewerActivity might be null (called by onDestroy) 47 if (mViewerActivity != null) { 48 // A new ViewerActivity is created; initialize its state and title 49 if (mStateJsonStr != null) { 50 mViewerActivity.setState(mStateJsonStr); 51 } 52 if (mTitle != null) { 53 mViewerActivity.setTitle(mTitle); 54 } 55 } 56 } 57 setTitle(String title)58 public void setTitle(String title) { 59 mTitle = title; // Similar to mStateJsonStr, we have to store this. 60 if (mTitle.startsWith("Viewer: ")) { // Quick hack to shorten the title 61 mTitle = mTitle.replaceFirst("Viewer: ", ""); 62 } 63 if (mViewerActivity != null) { 64 mViewerActivity.runOnUiThread(new Runnable() { 65 @Override 66 public void run() { 67 mViewerActivity.setTitle(mTitle); 68 } 69 }); 70 } 71 } 72 setState(String stateJsonStr)73 public void setState(String stateJsonStr) { 74 // We have to store this state because ViewerActivity may be destroyed while the native app 75 // is still running. When a new ViewerActivity is created, we'll pass the state to it. 76 mStateJsonStr = stateJsonStr; 77 if (mViewerActivity != null) { 78 mViewerActivity.runOnUiThread(new Runnable() { 79 @Override 80 public void run() { 81 mViewerActivity.setState(mStateJsonStr); 82 } 83 }); 84 } 85 } 86 } 87