replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / notification / examples / android / NotiConsumerExample / app / src / main / java / org / iotivity / service / ns / sample / consumer / LoginActivity.java
1 /*
2  * ******************************************************************
3  *
4  * Copyright 2016 Samsung Electronics All Rights Reserved.
5  *
6  * -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7  *
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
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  * -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21  */
22
23 package org.iotivity.service.ns.sample.consumer;
24
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;
37
38 /**
39  * This class is for login to the provider. Can be get auth code via web page.
40  */
41 public class LoginActivity extends Activity {
42     private static final String TAG           = "OIC_SIMPLE_LOGIN";
43
44     private final Context       context       = this;
45     private WebView             mWebView      = null;
46     private static String       loginAccount  = null;
47     private static String       mAuthProvider = null;
48
49     protected void onCreate(Bundle savedInstanceState) {
50         super.onCreate(savedInstanceState);
51         setContentView(R.layout.activity_login);
52
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);
61         }
62
63         Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
64         dialog.setCanceledOnTouchOutside(false);
65         dialogButton.setOnClickListener(new View.OnClickListener() {
66             @Override
67             public void onClick(View v) {
68                 dialog.dismiss();
69                 loginAccount = url.getText().toString();
70                 mAuthProvider = auth.getText().toString();
71
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());
77
78                 mWebView.loadUrl(loginAccount);
79             }
80         });
81         dialog.show();
82     }
83
84     public void onDestroy() {
85         super.onDestroy();
86     }
87
88     private class WebViewClientClass extends WebViewClient {
89
90         @Override
91         public void onPageFinished(WebView view, String url) {
92             Log.i(TAG,
93                     "onPageFinished!!! Response received: called url=" + url);
94
95             if (url.contains("code") && url.contains("code_expires_in")) {
96
97                 mWebView.setVisibility(View.INVISIBLE);
98
99                 // parsing url
100                 UrlQuerySanitizer sanitizer = new UrlQuerySanitizer();
101                 sanitizer.setAllowUnregisteredParamaters(true);
102                 sanitizer.parseUrl(url);
103
104                 String mAuthCode = sanitizer.getValue("code");
105                 Log.i(TAG, "onPageFinished!!! authCode=" + mAuthCode);
106
107                 Intent intent = getIntent();
108                 intent.putExtra("authCode", mAuthCode);
109                 intent.putExtra("authProvider", mAuthProvider);
110                 setResult(RESULT_OK, intent);
111
112                 finish();
113             }
114         }
115     }
116 }