blob: d204112cba29c29c2d684238234b5ca0634f97e0 [file] [log] [blame]
Adrien BĂ©raudffd32412012-08-07 18:39:23 -04001/*
2 * Copyright (C) 2004-2012 Savoir-Faire Linux Inc.
3 *
4 * Author: Adrien Beraud <adrien.beraud@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 * Additional permission under GNU GPL version 3 section 7:
21 *
22 * If you modify this program, or any covered work, by linking or
23 * combining it with the OpenSSL project's OpenSSL library (or a
24 * modified version of that library), containing parts covered by the
25 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
26 * grants you additional permission to convey the resulting work.
27 * Corresponding Source for a non-source form of such a combination
28 * shall include the source code for the parts of OpenSSL used as well
29 * as that of the covered work.
30 */
31package com.savoirfairelinux.sflphone.client;
32
33import android.app.ActionBar;
34import android.app.Activity;
35import android.app.Fragment;
36import android.app.FragmentManager;
37import android.app.FragmentTransaction;
38import android.os.Bundle;
39import android.support.v13.app.FragmentStatePagerAdapter;
40import android.support.v4.view.ViewPager;
41import android.view.Gravity;
42import android.view.LayoutInflater;
43import android.view.Menu;
44import android.view.View;
45import android.view.ViewGroup;
46import android.widget.TextView;
47
48import com.savoirfairelinux.sflphone.R;
49
50public class SFLPhoneHome extends Activity implements ActionBar.TabListener
51{
52 SectionsPagerAdapter mSectionsPagerAdapter;
53
54 /**
55 * The {@link ViewPager} that will host the section contents.
56 */
57 ViewPager mViewPager;
58
59 final private int[] icon_res_id = {R.drawable.ic_tab_call, R.drawable.ic_tab_history};
60
61 @Override
62 public void onCreate(Bundle savedInstanceState)
63 {
64 super.onCreate(savedInstanceState);
65 setContentView(R.layout.activity_sflphone_home);
66
67 mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
68
69 final ActionBar actionBar = getActionBar();
70 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
71
72 // Set up the ViewPager with the sections adapter.
73 mViewPager = (ViewPager) findViewById(R.id.pager);
74 mViewPager.setAdapter(mSectionsPagerAdapter);
75
76 // When swiping between different sections, select the corresponding tab.
77 // We can also use ActionBar.Tab#select() to do this if we have a reference to the
78 // Tab.
79 mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
80 {
81 @Override
82 public void onPageSelected(int position)
83 {
84 actionBar.setSelectedNavigationItem(position);
85 }
86 });
87
88 // For each of the sections in the app, add a tab to the action bar.
89 for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
90 // Create a tab with text corresponding to the page title defined by the adapter.
91 // Also specify this Activity object, which implements the TabListener interface, as the
92 // listener for when this tab is selected.
93 actionBar.addTab(actionBar.newTab().setIcon(icon_res_id[i]).setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
94 }
95 }
96
97 @Override
98 public boolean onCreateOptionsMenu(Menu menu)
99 {
100 getMenuInflater().inflate(R.menu.activity_sflphone_home, menu);
101 return true;
102 }
103
104 @Override
105 public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
106 {
107 }
108
109 @Override
110 public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
111 {
112 // When the given tab is selected, switch to the corresponding page in the ViewPager.
113 mViewPager.setCurrentItem(tab.getPosition());
114 }
115
116 @Override
117 public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
118 {
119 }
120
121 /**
122 * A {@link FragmentStatePagerAdapter} that returns a fragment corresponding to
123 * one of the primary sections of the app.
124 */
125 public class SectionsPagerAdapter extends FragmentStatePagerAdapter
126 {
127
128 public SectionsPagerAdapter(FragmentManager fm)
129 {
130 super(fm);
131 }
132
133 @Override
134 public Fragment getItem(int i)
135 {
136 Fragment fragment;
137 if(i == 0) {
138 fragment = new CallElementList();
139
140 } else
141 fragment = new DummySectionFragment();
142 Bundle args = new Bundle();
143 args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
144 fragment.setArguments(args);
145 return fragment;
146 }
147
148 @Override
149 public int getCount()
150 {
151 return 2;
152 }
153
154 @Override
155 public CharSequence getPageTitle(int position)
156 {
157 switch (position) {
158 case 0:
159 return getString(R.string.title_section1).toUpperCase();
160 case 1:
161 return getString(R.string.title_section2).toUpperCase();
162 }
163 return null;
164 }
165 }
166
167 /**
168 * A dummy fragment representing a section of the app, but that simply
169 * displays dummy text.
170 */
171 public static class DummySectionFragment extends Fragment
172 {
173 public DummySectionFragment()
174 {
175 setRetainInstance(true);
176 }
177
178 public static final String ARG_SECTION_NUMBER = "section_number";
179
180 @Override
181 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
182 {
183 TextView textView = new TextView(getActivity());
184 textView.setGravity(Gravity.CENTER);
185 Bundle args = getArguments();
186 textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER)));
187 return textView;
188 }
189
190 }
191}