diff options
Diffstat (limited to 'src/com/example/ti/ble/sensortag/ViewPagerActivity.java')
-rw-r--r-- | src/com/example/ti/ble/sensortag/ViewPagerActivity.java | 251 |
1 files changed, 251 insertions, 0 deletions
diff --git a/src/com/example/ti/ble/sensortag/ViewPagerActivity.java b/src/com/example/ti/ble/sensortag/ViewPagerActivity.java new file mode 100644 index 0000000..81edd52 --- /dev/null +++ b/src/com/example/ti/ble/sensortag/ViewPagerActivity.java | |||
@@ -0,0 +1,251 @@ | |||
1 | /************************************************************************************************** | ||
2 | Filename: ViewPagerActivity.java | ||
3 | Revised: $Date: 2013-09-09 16:23:36 +0200 (ma, 09 sep 2013) $ | ||
4 | Revision: $Revision: 27674 $ | ||
5 | |||
6 | Copyright (c) 2013 - 2014 Texas Instruments Incorporated | ||
7 | |||
8 | All rights reserved not granted herein. | ||
9 | Limited License. | ||
10 | |||
11 | Texas Instruments Incorporated grants a world-wide, royalty-free, | ||
12 | non-exclusive license under copyrights and patents it now or hereafter | ||
13 | owns or controls to make, have made, use, import, offer to sell and sell ("Utilize") | ||
14 | this software subject to the terms herein. With respect to the foregoing patent | ||
15 | license, such license is granted solely to the extent that any such patent is necessary | ||
16 | to Utilize the software alone. The patent license shall not apply to any combinations which | ||
17 | include this software, other than combinations with devices manufactured by or for TI (TI Devices). | ||
18 | No hardware patent is licensed hereunder. | ||
19 | |||
20 | Redistributions must preserve existing copyright notices and reproduce this license (including the | ||
21 | above copyright notice and the disclaimer and (if applicable) source code license limitations below) | ||
22 | in the documentation and/or other materials provided with the distribution | ||
23 | |||
24 | Redistribution and use in binary form, without modification, are permitted provided that the following | ||
25 | conditions are met: | ||
26 | |||
27 | * No reverse engineering, decompilation, or disassembly of this software is permitted with respect to any | ||
28 | software provided in binary form. | ||
29 | * any redistribution and use are licensed by TI for use only with TI Devices. | ||
30 | * Nothing shall obligate TI to provide you with source code for the software licensed and provided to you in object code. | ||
31 | |||
32 | If software source code is provided to you, modification and redistribution of the source code are permitted | ||
33 | provided that the following conditions are met: | ||
34 | |||
35 | * any redistribution and use of the source code, including any resulting derivative works, are licensed by | ||
36 | TI for use only with TI Devices. | ||
37 | * any redistribution and use of any object code compiled from the source code and any resulting derivative | ||
38 | works, are licensed by TI for use only with TI Devices. | ||
39 | |||
40 | Neither the name of Texas Instruments Incorporated nor the names of its suppliers may be used to endorse or | ||
41 | promote products derived from this software without specific prior written permission. | ||
42 | |||
43 | DISCLAIMER. | ||
44 | |||
45 | THIS SOFTWARE IS PROVIDED BY TI AND TIS LICENSORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, | ||
46 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | ||
47 | IN NO EVENT SHALL TI AND TIS LICENSORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
48 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, | ||
49 | OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
50 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
51 | POSSIBILITY OF SUCH DAMAGE. | ||
52 | |||
53 | |||
54 | **************************************************************************************************/ | ||
55 | package com.example.ti.ble.sensortag; | ||
56 | |||
57 | import java.util.ArrayList; | ||
58 | import java.util.List; | ||
59 | |||
60 | import android.app.ActionBar; | ||
61 | import android.app.Dialog; | ||
62 | import android.app.FragmentTransaction; | ||
63 | import android.os.Bundle; | ||
64 | import android.support.v4.app.Fragment; | ||
65 | import android.support.v4.app.FragmentActivity; | ||
66 | import android.support.v4.app.FragmentManager; | ||
67 | import android.support.v4.app.FragmentStatePagerAdapter; | ||
68 | import android.support.v4.view.ViewPager; | ||
69 | // import android.util.Log; | ||
70 | import android.view.Menu; | ||
71 | import android.view.MenuItem; | ||
72 | import android.widget.ImageView; | ||
73 | |||
74 | import com.example.ti.ble.sensortag.R; | ||
75 | |||
76 | public class ViewPagerActivity extends FragmentActivity { | ||
77 | // Constants | ||
78 | // private static final String TAG = "ViewPagerActivity"; | ||
79 | |||
80 | // GUI | ||
81 | protected static ViewPagerActivity mThis = null; | ||
82 | protected SectionsPagerAdapter mSectionsPagerAdapter; | ||
83 | private ViewPager mViewPager; | ||
84 | protected int mResourceFragmentPager; | ||
85 | protected int mResourceIdPager; | ||
86 | private int mCurrentTab = 0; | ||
87 | protected Menu optionsMenu; | ||
88 | private MenuItem refreshItem; | ||
89 | protected boolean mBusy; | ||
90 | |||
91 | protected ViewPagerActivity() { | ||
92 | // Log.d(TAG, "construct"); | ||
93 | mThis = this; | ||
94 | mBusy = false; | ||
95 | refreshItem = null; | ||
96 | } | ||
97 | |||
98 | @Override | ||
99 | protected void onCreate(Bundle savedInstanceState) { | ||
100 | // Log.d(TAG, "onCreate"); | ||
101 | super.onCreate(savedInstanceState); | ||
102 | setContentView(mResourceFragmentPager); | ||
103 | |||
104 | // Set up the action bar | ||
105 | final ActionBar actionBar = getActionBar(); | ||
106 | actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); | ||
107 | ImageView view = (ImageView) findViewById(android.R.id.home); | ||
108 | view.setPadding(10, 0, 20, 10); | ||
109 | |||
110 | // Set up the ViewPager with the sections adapter. | ||
111 | mViewPager = (ViewPager) findViewById(mResourceIdPager); | ||
112 | mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { | ||
113 | @Override | ||
114 | public void onPageSelected(int n) { | ||
115 | // Log.d(TAG, "onPageSelected: " + n); | ||
116 | actionBar.setSelectedNavigationItem(n); | ||
117 | } | ||
118 | }); | ||
119 | // Create the adapter that will return a fragment for each section | ||
120 | mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); | ||
121 | |||
122 | // Set up the ViewPager with the sections adapter. | ||
123 | mViewPager.setAdapter(mSectionsPagerAdapter); | ||
124 | } | ||
125 | |||
126 | |||
127 | @Override | ||
128 | public void onDestroy() { | ||
129 | super.onDestroy(); | ||
130 | // Log.d(TAG, "onDestroy"); | ||
131 | mSectionsPagerAdapter = null; | ||
132 | } | ||
133 | |||
134 | @Override | ||
135 | public void onBackPressed() { | ||
136 | if (mCurrentTab != 0) | ||
137 | getActionBar().setSelectedNavigationItem(0); | ||
138 | else | ||
139 | super.onBackPressed(); | ||
140 | } | ||
141 | |||
142 | @Override | ||
143 | public boolean onOptionsItemSelected(MenuItem item) { | ||
144 | // Log.d(TAG, "onOptionsItemSelected"); | ||
145 | // Handle presses on the action bar items | ||
146 | switch (item.getItemId()) { | ||
147 | // Respond to the action bar's Up/Home button | ||
148 | case android.R.id.home: | ||
149 | onBackPressed(); | ||
150 | return true; | ||
151 | default: | ||
152 | return super.onOptionsItemSelected(item); | ||
153 | } | ||
154 | } | ||
155 | |||
156 | protected void showBusyIndicator(final boolean busy) { | ||
157 | if (optionsMenu != null) { | ||
158 | refreshItem = optionsMenu.findItem(R.id.opt_progress); | ||
159 | if (refreshItem != null) { | ||
160 | if (busy) { | ||
161 | refreshItem.setActionView(R.layout.frame_progress); | ||
162 | } else { | ||
163 | refreshItem.setActionView(null); | ||
164 | } | ||
165 | refreshItem.setVisible(busy); | ||
166 | } else { | ||
167 | // Log.e(TAG,"Refresh item not expanded"); | ||
168 | } | ||
169 | } else { | ||
170 | // Log.e(TAG,"Options not expanded"); | ||
171 | } | ||
172 | mBusy = busy; | ||
173 | } | ||
174 | |||
175 | protected void refreshBusyIndicator() { | ||
176 | if (refreshItem == null) { | ||
177 | runOnUiThread(new Runnable() { | ||
178 | |||
179 | @Override | ||
180 | public void run() { | ||
181 | showBusyIndicator(mBusy); | ||
182 | } | ||
183 | }); | ||
184 | } | ||
185 | } | ||
186 | |||
187 | protected void openAboutDialog() { | ||
188 | final Dialog dialog = new AboutDialog(this); | ||
189 | dialog.show(); | ||
190 | } | ||
191 | |||
192 | public class SectionsPagerAdapter extends FragmentStatePagerAdapter { | ||
193 | private List<Fragment> mFragmentList; | ||
194 | private List<String> mTitles; | ||
195 | |||
196 | public SectionsPagerAdapter(FragmentManager fm) { | ||
197 | super(fm); | ||
198 | mFragmentList = new ArrayList<Fragment>(); | ||
199 | mTitles = new ArrayList<String>(); | ||
200 | } | ||
201 | |||
202 | public void addSection(Fragment fragment, String title) { | ||
203 | final ActionBar actionBar = getActionBar(); | ||
204 | mFragmentList.add(fragment); | ||
205 | mTitles.add(title); | ||
206 | actionBar.addTab(actionBar.newTab().setText(title).setTabListener(tabListener)); | ||
207 | notifyDataSetChanged(); | ||
208 | // Log.d(TAG, "Tab: " + title); | ||
209 | } | ||
210 | |||
211 | @Override | ||
212 | public Fragment getItem(int position) { | ||
213 | return mFragmentList.get(position); | ||
214 | } | ||
215 | |||
216 | @Override | ||
217 | public int getCount() { | ||
218 | return mTitles.size(); | ||
219 | } | ||
220 | |||
221 | @Override | ||
222 | public CharSequence getPageTitle(int position) { | ||
223 | if (position < getCount()) { | ||
224 | return mTitles.get(position); | ||
225 | } else { | ||
226 | return null; | ||
227 | } | ||
228 | } | ||
229 | } | ||
230 | |||
231 | // Create a tab listener that is called when the user changes tabs. | ||
232 | ActionBar.TabListener tabListener = new ActionBar.TabListener() { | ||
233 | |||
234 | public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { | ||
235 | int n = tab.getPosition(); | ||
236 | // Log.d(TAG, "onTabSelected: " + n); | ||
237 | mCurrentTab = n; | ||
238 | mViewPager.setCurrentItem(n); | ||
239 | } | ||
240 | |||
241 | public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { | ||
242 | // int n = tab.getPosition(); | ||
243 | // Log.d(TAG, "onTabUnselected: " + n); | ||
244 | } | ||
245 | |||
246 | public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { | ||
247 | // int n = tab.getPosition(); | ||
248 | // Log.d(TAG, "onTabReselected: " + n); | ||
249 | } | ||
250 | }; | ||
251 | } | ||