Migrate to gradle, appcompat as external dependency.
[mobilnebezpieczenstwo.git] / app / src / main / java / com / samsung / srpol / ui / tabpager / PageAdapter.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 java.util.List;
24
25 import android.content.Context;
26 import android.os.Bundle;
27 import android.support.v4.app.Fragment;
28 import android.support.v4.app.FragmentManager;
29 import android.support.v4.app.FragmentPagerAdapter;
30
31 import com.samsung.srpol.data.Category;
32 import com.samsung.srpol.loader.AppListLoader;
33
34 public class PageAdapter extends FragmentPagerAdapter {
35
36     private static List<Category> mCategories;
37     private static PageFragment[] mCreatedFragments = null;
38     
39     public PageAdapter(FragmentManager fm, Context context) {
40         super(fm);
41     }
42     
43     public void refreshPages() {
44         if (mCategories == null) {
45             mCategories = AppListLoader.getCategories();
46             notifyDataSetChanged();
47         } else {
48             refreshAdapterNotify();
49         }
50     }
51
52     public void refreshAdapterNotify() {
53         for (PageFragment fragment : mCreatedFragments) {
54             if (fragment != null)
55                 fragment.notifyDataSetChanged();
56         }
57     }
58     @Override
59     public Fragment getItem(int position) {
60         if (mCreatedFragments == null)
61             mCreatedFragments = new PageFragment[mCategories.size()];
62         PageFragment f = mCreatedFragments[position];
63         if (f == null) {
64             f = new PageFragment();
65             
66             Bundle args = new Bundle();
67             args.putInt("position", position);
68             f.setArguments(args);
69             mCreatedFragments[position] = f;
70         }
71         return f;
72     }
73
74     @Override
75     public CharSequence getPageTitle(int position) {
76         return mCategories.get(position).getTitle();
77     }
78     
79     @Override
80     public int getCount() {
81         return mCategories == null ? 0 : mCategories.size();
82     }
83     
84 }