Added README.txt and GPL Header to Source Files
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / fragments / PopupFragment.java
1 /* PopupFragment.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.DialogInterface;
18 import android.os.Bundle;
19 import android.support.v4.app.DialogFragment;
20 import android.support.v4.app.FragmentActivity;
21 import android.support.v4.app.FragmentTransaction;
22 import android.view.Gravity;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.widget.LinearLayout;
27 import android.widget.PopupWindow;
28
29 import de.dhbwloe.campusapp.CampusAppContext;
30 import de.dhbwloe.campusapp.CampusAppFragment;
31 import de.dhbwloe.campusapp.R;
32 import de.dhbwloe.campusapp.search.SearchIndices;
33 import de.dhbwloe.campusapp.search.SearchTarget;
34
35 /**
36  * Created by pk910 on 21.01.2016.
37  */
38 public class PopupFragment extends DialogFragment {
39     private CampusAppContext AppContext;
40     private CampusAppFragment oCurrentFragment;
41
42     @Override
43     public void onCreate(Bundle savedInstanceState) {
44         AppContext = CampusAppContext.getInstance();
45         super.onCreate(savedInstanceState);
46     }
47
48     @Override
49     public View onCreateView(LayoutInflater inflater, ViewGroup container,
50                              Bundle savedInstanceState) {
51         View view = inflater.inflate(R.layout.content_popup, container, false);
52         getDialog().setOnCancelListener(new DialogInterface.OnCancelListener() {
53             @Override
54             public void onCancel(DialogInterface dialog) {
55                 AppContext.getNavigationManager().closeDialog();
56             }
57         });
58
59         Bundle args = getArguments();
60         if(args != null) {
61             String targetPage = args.getString("target");
62             SearchTarget target = new SearchTarget(targetPage);
63             String targetPageName;
64             if(target.isInAppTarget())
65                 targetPageName = target.getTargetUrl();
66             else {
67                 targetPageName = "WebBrowser";
68                 args.putString("url", target.getTargetUrl());
69             }
70             CampusAppFragment fragment = AppContext.getNavigationManager().getPageFragment(targetPageName);
71             fragment.setArguments(args);
72
73             FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
74             transaction.add(R.id.content_container, fragment);
75             oCurrentFragment = fragment;
76             transaction.commit();
77         }
78
79         return view;
80     }
81
82     public void destroyView() {
83         if(oCurrentFragment != null) {
84             CampusAppFragment fragment = oCurrentFragment;
85             oCurrentFragment = null;
86             if(fragment.isAdded()) {
87                 FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
88                 transaction.remove(fragment);
89                 transaction.commit();
90             } else
91                 AppContext.getMainActivity().onBackPressed();
92         }
93     }
94
95     public CampusAppFragment getCurrentFragment() {
96         return oCurrentFragment;
97     }
98
99 }