Added README.txt and GPL Header to Source Files
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / search / SearchTarget.java
1 /* SearchTarget.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.search;
17
18 import android.os.Bundle;
19
20 import java.net.URLDecoder;
21 import java.net.URLEncoder;
22
23 import de.dhbwloe.campusapp.NavigationManager;
24
25 /**
26  * Created by pk910 on 19.01.2016.
27  */
28 public class SearchTarget {
29     private boolean bInAppTarget = false;
30     private String sTargetUrl = null;
31     private Bundle oArguments = new Bundle();
32
33     public SearchTarget(String target) {
34         if(target.startsWith("#")) { // InApp Navigation
35             bInAppTarget = true;
36             target = target.substring(1);
37         }
38         String[] parts = target.split("#", 2);
39         if(parts.length != 0)
40             sTargetUrl = parts[0];
41         if(parts.length > 1) {
42             // parse arguments?
43             String[] args = parts[1].split("&");
44             for(int i = 0; i < args.length; i++) {
45                 String[] arg = args[i].split("=");
46                 try {
47                     if (arg.length == 2)
48                         oArguments.putString(URLDecoder.decode(arg[0], "UTF-8"), URLDecoder.decode(arg[1], "UTF-8"));
49                     else
50                         oArguments.putBoolean(URLDecoder.decode(arg[0], "UTF-8"), true);
51                 } catch(Exception e) {}
52             }
53         }
54
55     }
56
57     public SearchTarget() {
58     }
59
60     public boolean isInAppTarget() {
61         return bInAppTarget;
62     }
63
64     public void setInAppTarget(boolean inAppTarget) {
65         bInAppTarget = inAppTarget;
66     }
67
68     public String getTargetUrl() {
69         return sTargetUrl;
70     }
71
72     public void setTargetUrl(String url) {
73         sTargetUrl = url;
74     }
75
76     public void setArgument(String name, String value) {
77         oArguments.putString(name, value);
78     }
79
80     public void navigate(NavigationManager navigationManager) {
81         Bundle args = oArguments;
82         if(bInAppTarget)
83             navigationManager.navigatePage(sTargetUrl, args);
84         else {
85             args.putString("url", sTargetUrl);
86             navigationManager.navigatePage("WebBrowser", args, false);
87         }
88     }
89 }