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