Merge remote-tracking branch 'origin/extended-easysetup'
[platform/upstream/iotivity.git] / service / easy-setup / sampleapp / mediator / android / EasySetup / app / src / main / java / org / iotivity / service / easysetup / LoginActivity.java
1 /**
2  * ***************************************************************
3  * <p/>
4  * Copyright 2015 Samsung Electronics All Rights Reserved.
5  * <p/>
6  * <p/>
7  * <p/>
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  * <p/>
12  * http://www.apache.org/licenses/LICENSE-2.0
13  * <p/>
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  * <p/>
20  * ****************************************************************
21  */
22
23 package org.iotivity.service.easysetup;
24
25 import android.app.Activity;
26 import android.app.AlertDialog;
27 import android.content.DialogInterface;
28 import android.content.Intent;
29 import android.content.SharedPreferences;
30 import android.net.ConnectivityManager;
31 import android.net.NetworkInfo;
32 import android.net.UrlQuerySanitizer;
33 import android.net.wifi.WifiConfiguration;
34 import android.net.wifi.WifiManager;
35 import android.os.Bundle;
36 import android.os.Handler;
37 import android.os.Message;
38 import android.preference.PreferenceManager;
39 import android.util.Log;
40 import android.view.View;
41 import android.view.View.OnClickListener;
42 import android.webkit.WebView;
43 import android.webkit.WebViewClient;
44 import android.widget.Button;
45 import android.widget.EditText;
46 import android.widget.LinearLayout;
47 import android.widget.ProgressBar;
48 import android.widget.RadioButton;
49 import android.widget.RelativeLayout;
50 import android.widget.TextView;
51 import android.widget.Toast;
52
53 import org.iotivity.base.ModeType;
54 import org.iotivity.base.OcException;
55 import org.iotivity.base.OcPlatform;
56 import org.iotivity.base.OcProvisioning;
57 import org.iotivity.base.PlatformConfig;
58 import org.iotivity.base.QualityOfService;
59 import org.iotivity.base.ServiceType;
60
61 import java.io.File;
62 import java.io.FileNotFoundException;
63 import java.io.FileOutputStream;
64 import java.io.IOException;
65 import java.io.InputStream;
66 import java.io.OutputStream;
67
68
69 public class LoginActivity extends Activity {
70     private static final String TAG = "Easysetup Login: ";
71
72     /* Samsung account */
73     private WebView mWebView = null;
74
75     @Override
76     protected void onCreate(Bundle savedInstanceState) {
77         super.onCreate(savedInstanceState);
78         setContentView(R.layout.activity_login);
79
80         /* For Samsung account authentifaction */
81         mWebView = (WebView) findViewById(R.id.webView);
82         mWebView.setVisibility(View.VISIBLE);
83         mWebView.setInitialScale(200);
84         mWebView.getSettings().setJavaScriptEnabled(true);
85         //mWebView.getSettings().setSupportZoom(true);
86         mWebView.getSettings().setBuiltInZoomControls(true);
87         mWebView.setWebViewClient(new WebViewClientClass());
88
89         mWebView.loadUrl("https://github.com/login?return_to=%2Flogin%2Foauth%2Fauthorize%3Fclient_id%3Dea9c18f540323b0213d0%26redirect_uri%3Dhttp%253A%252F%252Fwww.example.com%252Foauth_callback%252F");
90     }
91
92     public void onDestroy() {
93         super.onDestroy();
94     }
95
96     private class WebViewClientClass extends WebViewClient {
97
98         @Override
99         public void onPageFinished(WebView view, String url) {
100             Log.e(TAG, "called!!! url=" + url);
101
102             if(url.contains("http://www.example.com/oauth_callback/")){
103
104                 mWebView.setVisibility(View.INVISIBLE);
105
106                 //parsing url
107                 UrlQuerySanitizer sanitizer = new UrlQuerySanitizer();
108                 sanitizer.setAllowUnregisteredParamaters(true);
109                 sanitizer.parseUrl(url);
110
111                 String mAuthCode = null;
112                 mAuthCode = sanitizer.getValue("code");
113
114                 Intent intent = getIntent();
115                 intent.putExtra("authCode", mAuthCode);
116                 intent.putExtra("authProvider", "github");
117
118                 setResult(RESULT_OK, intent);
119
120                 finish();
121             }
122         }
123     }
124 }