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