Upstream version 9.38.204.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / core_internal / src / org / xwalk / core / internal / extension / api / presentation / XWalkPresentationContent.java
1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.xwalk.core.internal.extension.api.presentation;
6
7 import java.lang.ref.WeakReference;
8
9 import android.app.Activity;
10 import android.content.Context;
11 import android.os.Bundle;
12 import android.view.View;
13
14 import org.xwalk.core.internal.XWalkClient;
15 import org.xwalk.core.internal.XWalkUIClientInternal;
16 import org.xwalk.core.internal.XWalkViewInternal;
17
18 /**
19  * Represents the content to be presented on the secondary display.
20  */
21 public class XWalkPresentationContent {
22     public final int INVALID_PRESENTATION_ID = -1;
23
24     private int mPresentationId = INVALID_PRESENTATION_ID;
25     private XWalkViewInternal mContentView;
26     private Context mContext;
27     private WeakReference<Activity> mActivity;
28     private PresentationDelegate mDelegate;
29
30
31     public XWalkPresentationContent(
32             Context context, WeakReference<Activity> activity, PresentationDelegate delegate) {
33         mContext = context;
34         mActivity = activity;
35         mDelegate = delegate;
36     }
37
38     public void load(final String url) {
39         Activity activity = mActivity.get();
40         if (activity == null) return;
41
42         if (mContentView == null) {
43             mContentView = new XWalkViewInternal(mContext, activity);
44             final XWalkUIClientInternal xWalkUIClient = new XWalkUIClientInternal(mContentView) {
45                 @Override
46                 public void onJavascriptCloseWindow(XWalkViewInternal view) {
47                     // The content was closed already. Web need to invalidate the
48                     // presentation id now.
49                     mPresentationId = INVALID_PRESENTATION_ID;
50                     onContentClosed();
51                 }
52
53                 @Override
54                 public void onPageLoadStopped(
55                         XWalkViewInternal view, String url, LoadStatusInternal status) {
56                     if (status == LoadStatusInternal.FINISHED) {
57                         mPresentationId = mContentView.getContentID();
58                         onContentLoaded();
59                     }
60                 }
61             };
62             mContentView.setUIClient(xWalkUIClient);
63         }
64         mContentView.load(url, null);
65     }
66
67     public int getPresentationId() {
68         return mPresentationId;
69     }
70
71     public View getContentView() {
72         return mContentView;
73     }
74
75     public void close() {
76         mContentView.onDestroy();
77         mPresentationId = INVALID_PRESENTATION_ID;
78         mContentView = null;
79     }
80
81     public void onPause() {
82         mContentView.pauseTimers();
83         mContentView.onHide();
84     }
85
86     public void onResume() {
87         mContentView.resumeTimers();
88         mContentView.onShow();
89     }
90
91     private void onContentLoaded() {
92         if (mDelegate != null) mDelegate.onContentLoaded(this);
93     }
94
95     private void onContentClosed() {
96         if (mDelegate != null) mDelegate.onContentClosed(this);
97     }
98
99     /**
100      * Interface to hook into XWalkPresentationContent instance.
101      */
102     public interface PresentationDelegate {
103         /**
104          * Called when the presentation content is loaded.
105          */
106         public void onContentLoaded(XWalkPresentationContent content);
107
108         /**
109          * Called when the presentation content is closed.
110          */
111         public void onContentClosed(XWalkPresentationContent content);
112     }
113 }