Initial commit.
[mobilnebezpieczenstwo.git] / src / com / samsung / srpol / ui / tabpager / PageFragment.java
1 /*
2    Copyright (C) 2014  Samsung Electronics Polska Sp. z o.o.
3
4     This program is free software: you can redistribute it and/or modify
5     it under the terms of the GNU AFFERO General Public License as published by
6     the Free Software Foundation, either version 3 of the License, or
7     (at your option) any later version.
8     You may obtain a copy of the License at
9
10                 http://www.gnu.org/licenses/agpl-3.0.txt
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 package com.samsung.srpol.ui.tabpager;
22
23 import com.nhaarman.listviewanimations.appearance.AnimationAdapter;
24 import com.nhaarman.listviewanimations.appearance.simple.AlphaInAnimationAdapter;
25 import com.samsung.srpol.R;
26 import com.samsung.srpol.data.Category;
27 import com.samsung.srpol.data.Subcategory;
28 import com.samsung.srpol.loader.AppListLoader;
29 import com.samsung.srpol.parallax.ParallaxListView;
30 import com.samsung.srpol.ui.AppInfoActivity;
31 import com.samsung.srpol.ui.PopupActivity;
32
33 import android.content.Intent;
34 import android.os.Bundle;
35 import android.support.v4.app.Fragment;
36 import android.view.LayoutInflater;
37 import android.view.View;
38 import android.view.ViewGroup;
39 import android.view.View.OnClickListener;
40 import android.widget.AdapterView;
41 import android.widget.ImageButton;
42 import android.widget.ImageView;
43 import android.widget.LinearLayout;
44 import android.widget.ListView;
45 import android.widget.TextView;
46 import android.widget.AdapterView.OnItemClickListener;
47
48 public class PageFragment extends Fragment {
49
50     private AppListArrayAdapter mAppListArrayAdapter;
51     private Category mCategory;
52     private int mPosition;
53
54     @Override
55     public void onCreate(Bundle savedInstanceState) {
56         super.onCreate(savedInstanceState);
57         mPosition = getArguments() != null ? getArguments().getInt("position") : 0;
58         setRetainInstance(true);
59     }
60
61     @Override
62     public View onCreateView(LayoutInflater inflater, ViewGroup container,
63             Bundle savedInstanceState) {
64         View rootView = inflater.inflate(R.layout.fragment_main, container, false);
65         
66         LinearLayout headerLayout = (LinearLayout) inflater.inflate(R.layout.header_layout, container, false);
67         headerLayout.setLayoutParams(new ListView.LayoutParams(
68                 ListView.LayoutParams.MATCH_PARENT, ListView.LayoutParams.WRAP_CONTENT));
69         if (mCategory == null) {
70             if (AppListLoader.getCategories() != null) {
71                 mCategory = AppListLoader.getCategories().get(mPosition);
72             } else {
73                 return rootView;
74             }
75         }
76         TextView header = (TextView) headerLayout.findViewById(R.id.section_label);
77         if (header != null) {
78             header.setText(mCategory.getDescription());
79         }
80         
81         TextView headerLabel = (TextView) rootView.findViewById(R.id.header_label);
82         if (headerLabel != null) {
83             String titleSufix = getActivity().getString(R.string.apps_that);
84             headerLabel.setText(titleSufix.concat(mCategory.getHeader()));
85         }
86         
87         //Header icons Click
88         LinearLayout groupIcons = (LinearLayout) headerLayout.findViewById(R.id.group_icons);
89         for (Subcategory subgroup : mCategory.getSubCategories()) {
90             ImageView icon = new ImageView(getActivity());
91             icon.setImageDrawable(subgroup.getIconDrawable());
92             groupIcons.addView(icon);
93         }
94
95         //Header More click
96         ImageButton moreButton = (ImageButton) headerLayout.findViewById(R.id.legend_btn);
97         moreButton.setOnClickListener(new OnClickListener() {
98
99             @Override
100             public void onClick(View arg0) {
101                 Intent intent = new Intent(getActivity(), PopupActivity.class);
102                 intent.putExtra(PopupActivity.POPUP_CATEGORY, mPosition);
103                 getActivity().startActivity(intent);
104             }
105         });
106
107         // List with applications
108         ParallaxListView listview = (ParallaxListView) rootView.findViewById(R.id.listview);
109         listview.addParallaxedHeaderView(headerLayout);
110         mAppListArrayAdapter = new AppListArrayAdapter(getActivity(), mCategory);
111         AnimationAdapter adapter = new AlphaInAnimationAdapter(mAppListArrayAdapter);
112         adapter.setAbsListView(listview);
113         listview.setAdapter(adapter);
114         listview.setOnItemClickListener(new OnItemClickListener() {
115
116             @Override
117             public void onItemClick(AdapterView<?> parent, View view,
118                     int positionInAdapter, long id) {
119                 Intent intent = new Intent(getActivity(), AppInfoActivity.class);
120                 intent.putExtra(AppInfoActivity.APP_PACKAGE_NAME,
121                         mAppListArrayAdapter.getItem(positionInAdapter - 1).getAppPackageName());
122                 getActivity().startActivity(intent);
123             }
124         });
125         
126         return rootView;
127     }
128
129     public void notifyDataSetChanged() {
130         mAppListArrayAdapter.notifyDataSetChanged();
131     }
132
133 }