OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 package org.skia.viewer; | 8 package org.skia.viewer; |
9 | 9 |
10 import android.app.Application; | 10 import android.app.Application; |
11 | 11 |
12 public class ViewerApplication extends Application { | 12 public class ViewerApplication extends Application { |
13 private long mNativeHandle = 0; | 13 private long mNativeHandle = 0; |
14 private ViewerActivity mViewerActivity; | 14 private ViewerActivity mViewerActivity; |
| 15 private String mStateJsonStr, mTitle; |
15 | 16 |
16 static { | 17 static { |
17 System.loadLibrary("skia_android"); | 18 System.loadLibrary("skia_android"); |
18 System.loadLibrary("viewer"); | 19 System.loadLibrary("viewer"); |
19 } | 20 } |
20 | 21 |
21 private native long createNativeApp(); | 22 private native long createNativeApp(); |
22 private native void destroyNativeApp(long handle); | 23 private native void destroyNativeApp(long handle); |
23 | 24 |
24 @Override | 25 @Override |
25 public void onCreate() { | 26 public void onCreate() { |
26 super.onCreate(); | 27 super.onCreate(); |
27 mNativeHandle = createNativeApp(); | 28 mNativeHandle = createNativeApp(); |
28 } | 29 } |
29 | 30 |
30 @Override | 31 @Override |
31 public void onTerminate() { | 32 public void onTerminate() { |
32 if (mNativeHandle != 0) { | 33 if (mNativeHandle != 0) { |
33 destroyNativeApp(mNativeHandle); | 34 destroyNativeApp(mNativeHandle); |
34 mNativeHandle = 0; | 35 mNativeHandle = 0; |
35 } | 36 } |
36 super.onTerminate(); | 37 super.onTerminate(); |
37 } | 38 } |
38 | 39 |
39 public long getNativeHandle() { | 40 public long getNativeHandle() { |
40 return mNativeHandle; | 41 return mNativeHandle; |
41 } | 42 } |
42 | 43 |
43 public void setViewerActivity(ViewerActivity viewerActivity) { | 44 public void setViewerActivity(ViewerActivity viewerActivity) { |
44 this.mViewerActivity = 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 } |
45 } | 56 } |
46 | 57 |
47 public void setTitle(String title) { | 58 public void setTitle(String title) { |
48 final String finalTitle = title; | 59 mTitle = title; // Similar to mStateJsonStr, we have to store this. |
49 if (mViewerActivity != null) { | 60 if (mViewerActivity != null) { |
50 mViewerActivity.runOnUiThread(new Runnable() { | 61 mViewerActivity.runOnUiThread(new Runnable() { |
51 @Override | 62 @Override |
52 public void run() { | 63 public void run() { |
53 mViewerActivity.setTitle(finalTitle); | 64 mViewerActivity.setTitle(mTitle); |
54 } | 65 } |
55 }); | 66 }); |
56 } | 67 } |
| 68 } |
| 69 |
| 70 public void setState(String stateJsonStr) { |
| 71 // We have to store this state because ViewerActivity may be destroyed w
hile the native app |
| 72 // is still running. When a new ViewerActivity is created, we'll pass th
e state to it. |
| 73 mStateJsonStr = stateJsonStr; |
| 74 if (mViewerActivity != null) { |
| 75 mViewerActivity.runOnUiThread(new Runnable() { |
| 76 @Override |
| 77 public void run() { |
| 78 mViewerActivity.setState(mStateJsonStr); |
| 79 } |
| 80 }); |
| 81 } |
57 } | 82 } |
58 } | 83 } |
OLD | NEW |