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.provider;
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;
50 protected void onCreate(Bundle savedInstanceState) {
51 super.onCreate(savedInstanceState);
52 setContentView(R.layout.activity_login);
54 final Dialog dialog = new Dialog(context);
55 dialog.setContentView(R.layout.dialog_auth);
56 dialog.setTitle("Login Details");
57 final EditText auth = (EditText) dialog.findViewById(R.id.EditTextAuth);
58 final EditText url = (EditText) dialog.findViewById(R.id.EditTextUrl);
59 if (loginAccount != null && mAuthProvider != null) {
60 url.setText(loginAccount);
61 auth.setText(mAuthProvider);
64 Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
65 dialog.setCanceledOnTouchOutside(false);
66 dialogButton.setOnClickListener(new View.OnClickListener() {
68 public void onClick(View v) {
70 loginAccount = url.getText().toString();
71 mAuthProvider = auth.getText().toString();
73 mWebView = (WebView) findViewById(R.id.webView);
74 mWebView.setInitialScale(200);
75 mWebView.getSettings().setJavaScriptEnabled(true);
76 mWebView.getSettings().setBuiltInZoomControls(true);
77 mWebView.setWebViewClient(new WebViewClientClass());
79 mWebView.loadUrl(loginAccount);
85 public void onDestroy() {
89 private class WebViewClientClass extends WebViewClient {
92 public void onPageFinished(WebView view, String url) {
94 "onPageFinished!!! Response received: called url=" + url);
96 if (url.contains("code") && url.contains("code_expires_in")) {
98 mWebView.setVisibility(View.INVISIBLE);
101 UrlQuerySanitizer sanitizer = new UrlQuerySanitizer();
102 sanitizer.setAllowUnregisteredParamaters(true);
103 sanitizer.parseUrl(url);
105 String mAuthCode = sanitizer.getValue("code");
106 Log.i(TAG, "onPageFinished!!! authCode=" + mAuthCode);
108 Intent intent = getIntent();
109 intent.putExtra("authCode", mAuthCode);
110 intent.putExtra("authProvider", mAuthProvider);
111 setResult(RESULT_OK, intent);