Upstream version 8.36.156.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / core / src / org / xwalk / core / 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.extension.api.presentation;
6
7 import android.app.Activity;
8 import android.content.Context;
9 import android.os.Bundle;
10 import android.view.View;
11
12 import org.xwalk.core.XWalkClient;
13 import org.xwalk.core.XWalkUIClient;
14 import org.xwalk.core.XWalkView;
15
16 /**
17  * Represents the content to be presented on the secondary display.
18  */
19 public class XWalkPresentationContent {
20     public final int INVALID_PRESENTATION_ID = -1;
21
22     private int mPresentationId = INVALID_PRESENTATION_ID;
23     private XWalkView mContentView;
24     private Context mContext;
25     private Activity mActivity;
26     private PresentationDelegate mDelegate;
27
28
29     public XWalkPresentationContent(Context context, Activity activity, PresentationDelegate delegate) {
30         mContext = context;
31         mActivity = activity;
32         mDelegate = delegate;
33     }
34
35     public void load(final String url) {
36         if (mContentView == null) {
37             mContentView = new XWalkView(mContext, mActivity);
38             final XWalkClient xWalkClient = new XWalkClient(mContentView) {
39                 @Override
40                 public void onPageFinished(XWalkView view, String url) {
41                     mPresentationId = mContentView.getContentID();
42                     onContentLoaded();
43                 }
44             };
45             mContentView.setXWalkClient(xWalkClient);
46
47             final XWalkUIClient xWalkUIClient = new XWalkUIClient(mContentView) {
48                 @Override
49                 public void onJavascriptCloseWindow(XWalkView view) {
50                     // The content was closed already. Web need to invalidate the
51                     // presentation id now.
52                     mPresentationId = INVALID_PRESENTATION_ID;
53                     onContentClosed();
54                 }
55             };
56             mContentView.setUIClient(xWalkUIClient);
57         }
58         mContentView.load(url, null);
59     }
60
61     public int getPresentationId() {
62         return mPresentationId;
63     }
64
65     public View getContentView() {
66         return mContentView;
67     }
68
69     public void close() {
70         mContentView.onDestroy();
71         mPresentationId = INVALID_PRESENTATION_ID;
72         mContentView = null;
73     }
74
75     public void onPause() {
76         mContentView.pauseTimers();
77         mContentView.onHide();
78     }
79
80     public void onResume() {
81         mContentView.resumeTimers();
82         mContentView.onShow();
83     }
84
85     private void onContentLoaded() {
86         if (mDelegate != null) mDelegate.onContentLoaded(this);
87     }
88
89     private void onContentClosed() {
90         if (mDelegate != null) mDelegate.onContentClosed(this);
91     }
92
93     /**
94      * Interface to hook into XWalkPresentationContent instance.
95      */
96     public interface PresentationDelegate {
97         /**
98          * Called when the presentation content is loaded.
99          */
100         public void onContentLoaded(XWalkPresentationContent content);
101
102         /**
103          * Called when the presentation content is closed.
104          */
105         public void onContentClosed(XWalkPresentationContent content);
106     }
107 }