Added README.txt and GPL Header to Source Files
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / fragments / WebBrowser.java
1 /* WebBrowser.java
2  *
3  * This program is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 package de.dhbwloe.campusapp.fragments;
17 import android.content.Intent;
18 import android.net.Uri;
19 import android.os.Bundle;
20 import android.support.v4.app.Fragment;
21 import android.view.LayoutInflater;
22 import android.view.View;
23 import android.view.ViewGroup;
24 import android.webkit.WebView;
25
26 import de.dhbwloe.campusapp.CampusAppFragment;
27 import de.dhbwloe.campusapp.R;
28
29 public class WebBrowser extends CampusAppFragment {
30     private boolean bRedirectedToBrowser = false;
31     private boolean bBrowserRunning = false;
32
33     @Override
34     public View onCreateView(LayoutInflater inflater, ViewGroup container,
35                              Bundle savedInstanceState) {
36         View view = inflater.inflate(R.layout.fragment_web_browser, container, false);
37         WebView webview = (WebView) view.findViewById(R.id.browserWebView);
38
39         Bundle args = getArguments();
40         if(args == null) {
41             AppContext.getNavigationManager().navigatePage("Dashboard", null, false);
42             return null;
43         }
44
45         String str;
46         if((str = args.getString("html")) != null) {
47             showWebViewHtml(webview, str);
48         }
49         else if((str = args.getString("url")) != null) {
50             Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(str));
51             startActivity(browserIntent);
52
53             bRedirectedToBrowser = true;
54             return null;
55         }
56
57         return view;
58     }
59
60     private void showWebViewHtml(WebView webview, String html) {
61         webview.loadData(html, "text/html", null);
62     }
63
64     @Override
65     public  void onPause() {
66         super.onPause();
67
68         if(bRedirectedToBrowser)
69             bBrowserRunning = true;
70     }
71
72     @Override
73     public void onResume() {
74         super.onResume();
75
76         if(bRedirectedToBrowser && bBrowserRunning) {
77             AppContext.getMainActivity().onBackPressed();
78         }
79     }
80
81 }