Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e3c25adee31da6cdaf2e2c2743da03747f7af336 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*******************************************************************************
 * Copyright (c) 2013, 2017 Orange.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * Contributors:
 *    BAREAU Cyrille <cyrille.bareau@orange.com>, 
 *    BONNARDEL Gregory <gbonnardel.ext@orange.com>, 
 *    BOLLE Sebastien <sebastien.bolle@orange.com>.
 *******************************************************************************/
package org.eclipse.om2m.android.dashboard.applications;

import java.util.Locale;

import org.eclipse.om2m.android.dashboard.cse.OneM2MListener;
import org.eclipse.om2m.android.dashboard.cse.OneM2MRequester;
import org.eclipse.om2m.android.dashboard.cse.OneM2MRequest.OneM2MReqType;
import org.eclipse.om2m.android.dashboard.cse.models.OneM2MApplication;
import org.eclipse.om2m.android.dashboard.cse.requests.OneM2MRequestParams;
import org.eclipse.om2m.android.dashboard.tools.SettingsManager;

import org.eclipse.om2m.android.dashboard.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.webkit.HttpAuthHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("SetJavaScriptEnabled")
public class ApplicationConfigActivity extends Activity implements OneM2MListener {
	
	static private final String PARAM = "___RES___";
	static private final String MSG_TEMPLATE = "<html><head><style type=\"text/css\">" + 
			"#maindiv {width:100%; height:100%; text-align:center; vertical-align:middle; border:1px solid #be1f2e;}" +
			"#content {width:80%; height:80p%; margin-left:auto; margin-right:auto; position:relative; top:40%;}" +	
			"h3 {color:#be1f2e; text-align:center; vertical-align:middle}" + 
			"p {color:black; text-align:center; vertical-align:middle}" + 
			"</style></head>" +
			"<body><div id=\"maindiv\"><div id=\"content\"><h3>" + 
			PARAM + 
			"</h3></div></div></body></html>";

	private WebView configContentView;
	private TextView configTitle;

	private String applicationId;
	private OneM2MApplication application;
	
	// =====================================================================================
	// Lifecycle
	// =====================================================================================

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			applicationId = extras.getString("applicationId");				
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		
		String language = SettingsManager.getInstance(this).getLanguage();
		Resources res = getApplication().getResources();
		Configuration config = res.getConfiguration();
		config.locale = new Locale(language);
		res.updateConfiguration(config, null);

		setContentView(R.layout.application_config_activity);
		
		configTitle = (TextView)findViewById(R.id.tv_application_config_title);
		configContentView = (WebView)findViewById(R.id.wv_application_config_container);

		// Retrieve the application
		OneM2MRequester.getInstance().sendRequest(OneM2MReqType.APPLICATION, 
				new OneM2MRequestParams(applicationId), getApplicationContext(), this);

		setBanner("");
	}

	private void createWebView() {
		setBanner(application.getApn());
		final String name = SettingsManager.getInstance(this).getCSELogin();
		final String password = SettingsManager.getInstance(this).getCSEPwd();
		final String url = application.getPresentationUrl() 
				+ "?name=" + name + "&password=" + password;

		configContentView.setWebViewClient(new WebViewClient () {
			public void onReceivedHttpAuthRequest(WebView view,
					HttpAuthHandler handler, String host, String realm) {
				try {
					if (handler.useHttpAuthUsernamePassword()) {
						handler.proceed(name, password);
					} else {
	            		setBanner("Error authentication.");
						String content = MSG_TEMPLATE.replaceFirst(PARAM, 
								getResources().getString(R.string.application_details_no_access_rights));
						view.loadData(content, "text/html", "UTF-8");
					}
            	} catch (Exception e) {
            		setBanner("Error authentication: " + e.getMessage());
					String content = MSG_TEMPLATE.replaceFirst(PARAM, 
							getResources().getString(R.string.application_details_no_access_rights)) + ":" + e.getMessage();
					view.loadData(content, "text/html", "UTF-8");
				}
			}
		});
		configContentView.getSettings().setJavaScriptEnabled(true);
		configContentView.resumeTimers();

		configContentView.setWebChromeClient(new WebChromeClient() {
			@Override
			public void onReceivedTitle(WebView view, String title) {
				super.onReceivedTitle(view, title);
				if (title.contains("404")) {
					String content = MSG_TEMPLATE.replaceFirst(PARAM, 
							getResources().getString(R.string.application_details_no_config));
					view.loadData(content, "text/html", "UTF-8");
				} else if (title.contains("401")) {
					if (title.contains("_OTB_reset_User__")) {
						view.loadUrl(url);
					} else {
						String content = MSG_TEMPLATE.replaceFirst(PARAM, 
								getResources().getString(R.string.application_details_no_access_rights));
						view.loadData(content, "text/html", "UTF-8");
					}
				}
			}
		});
		configContentView.loadUrl(url);
	}
	
	private final void setBanner(String msg) {
		configTitle.setText(getResources().getString(R.string.application_config_title)
				.replace("%app_name%", msg));
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.i(getClass().getName(), "onDestroy");
		configContentView.clearHistory();
		configContentView.clearCache(true);
		configContentView.loadUrl("about:blank");
		configContentView.freeMemory(); 
		configContentView.pauseTimers();
		configContentView = null;
	}

	@Override
	public void onOneM2MResponse(OneM2MReqType requestType, Object response) {
		if (requestType == OneM2MReqType.APPLICATION) {
			application = (OneM2MApplication)response;
			if (application != null)
				createWebView();
		}
	}

	@Override
	public void onOneM2MError(OneM2MReqType requestType, String msg) {
		Toast.makeText(this, "getApplication " + msg, Toast.LENGTH_LONG).show();
	}
	
}

Back to the top