replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / notification / examples / android / NotiProviderExample / app / src / main / java / org / iotivity / service / ns / sample / provider / 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.provider;
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     @Override
50     protected void onCreate(Bundle savedInstanceState) {
51         super.onCreate(savedInstanceState);
52         setContentView(R.layout.activity_login);
53
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);
62         }
63
64         Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
65         dialog.setCanceledOnTouchOutside(false);
66         dialogButton.setOnClickListener(new View.OnClickListener() {
67             @Override
68             public void onClick(View v) {
69                 dialog.dismiss();
70                 loginAccount = url.getText().toString();
71                 mAuthProvider = auth.getText().toString();
72
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());
78
79                 mWebView.loadUrl(loginAccount);
80             }
81         });
82         dialog.show();
83     }
84
85     public void onDestroy() {
86         super.onDestroy();
87     }
88
89     private class WebViewClientClass extends WebViewClient {
90
91         @Override
92         public void onPageFinished(WebView view, String url) {
93             Log.i(TAG,
94                     "onPageFinished!!! Response received: called url=" + url);
95
96             if (url.contains("code") && url.contains("code_expires_in")) {
97
98                 mWebView.setVisibility(View.INVISIBLE);
99
100                 // parsing url
101                 UrlQuerySanitizer sanitizer = new UrlQuerySanitizer();
102                 sanitizer.setAllowUnregisteredParamaters(true);
103                 sanitizer.parseUrl(url);
104
105                 String mAuthCode = sanitizer.getValue("code");
106                 Log.i(TAG, "onPageFinished!!! authCode=" + mAuthCode);
107
108                 Intent intent = getIntent();
109                 intent.putExtra("authCode", mAuthCode);
110                 intent.putExtra("authProvider", mAuthProvider);
111                 setResult(RESULT_OK, intent);
112
113                 finish();
114             }
115         }
116     }
117 }