2 * ******************************************************************
4 * Copyright 2016 Samsung Electronics All Rights Reserved.
6 * -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23 package org.iotivity.service.ns.sample.consumer;
25 import android.app.Activity;
26 import android.app.Dialog;
27 import android.content.Context;
28 import android.content.Intent;
29 import android.net.UrlQuerySanitizer;
30 import android.os.Bundle;
31 import android.util.Log;
32 import android.view.View;
33 import android.webkit.WebView;
34 import android.webkit.WebViewClient;
35 import android.widget.Button;
36 import android.widget.EditText;
39 * This class is for login to the provider. Can be get auth code via web page.
41 public class LoginActivity extends Activity {
42 private static final String TAG = "OIC_SIMPLE_LOGIN";
44 private final Context context = this;
45 private WebView mWebView = null;
46 private static String loginAccount = null;
47 private static String mAuthProvider = null;
49 protected void onCreate(Bundle savedInstanceState) {
50 super.onCreate(savedInstanceState);
51 setContentView(R.layout.activity_login);
53 final Dialog dialog = new Dialog(context);
54 dialog.setContentView(R.layout.dialog_auth);
55 dialog.setTitle("Login Details");
56 final EditText auth = (EditText) dialog.findViewById(R.id.EditTextAuth);
57 final EditText url = (EditText) dialog.findViewById(R.id.EditTextUrl);
58 if (loginAccount != null && mAuthProvider != null) {
59 url.setText(loginAccount);
60 auth.setText(mAuthProvider);
63 Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
64 dialog.setCanceledOnTouchOutside(false);
65 dialogButton.setOnClickListener(new View.OnClickListener() {
67 public void onClick(View v) {
69 loginAccount = url.getText().toString();
70 mAuthProvider = auth.getText().toString();
72 mWebView = (WebView) findViewById(R.id.webView);
73 mWebView.setInitialScale(200);
74 mWebView.getSettings().setJavaScriptEnabled(true);
75 mWebView.getSettings().setBuiltInZoomControls(true);
76 mWebView.setWebViewClient(new WebViewClientClass());
78 mWebView.loadUrl(loginAccount);
84 public void onDestroy() {
88 private class WebViewClientClass extends WebViewClient {
91 public void onPageFinished(WebView view, String url) {
93 "onPageFinished!!! Response received: called url=" + url);
95 if (url.contains("code") && url.contains("code_expires_in")) {
97 mWebView.setVisibility(View.INVISIBLE);
100 UrlQuerySanitizer sanitizer = new UrlQuerySanitizer();
101 sanitizer.setAllowUnregisteredParamaters(true);
102 sanitizer.parseUrl(url);
104 String mAuthCode = sanitizer.getValue("code");
105 Log.i(TAG, "onPageFinished!!! authCode=" + mAuthCode);
107 Intent intent = getIntent();
108 intent.putExtra("authCode", mAuthCode);
109 intent.putExtra("authProvider", mAuthProvider);
110 setResult(RESULT_OK, intent);