]> rtime.felk.cvut.cz Git - orte.git/blob - orte/contrib/shape_android/src/org/ocera/orte/shape4a/MainActivity.java
7de0d7bd573f2b0e839064c207c114da6fe16ad2
[orte.git] / orte / contrib / shape_android / src / org / ocera / orte / shape4a / MainActivity.java
1 /**
2  * 
3  *      This file is part of shape4a.
4  *
5  *  shape4a is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  shape4a is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with shape4a.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 package org.ocera.orte.shape4a;
20
21 import java.util.regex.Pattern;
22
23 import android.support.v7.app.ActionBarActivity;
24 import android.support.v4.app.DialogFragment;
25 import android.support.v4.app.Fragment;
26 import android.support.v4.app.FragmentManager;
27 import android.support.v4.app.FragmentPagerAdapter;
28 import android.app.AlertDialog;
29 import android.app.Dialog;
30 import android.content.Context;
31 import android.content.DialogInterface;
32 import android.net.wifi.WifiManager;
33 import android.net.wifi.WifiManager.WifiLock;
34 import android.os.Bundle;
35 import android.support.v4.view.ViewPager;
36 import android.text.InputType;
37 import android.text.format.Formatter;
38 import android.view.Menu;
39 import android.view.MenuItem;
40 import android.view.View;
41 import android.widget.EditText;
42 import android.widget.LinearLayout;
43
44 import org.ocera.orte.DomainApp;
45 import org.ocera.orte.Manager;
46
47 //TODO Better navigation (swipe sometimes miss drag).
48
49 /**
50  * Main application activity, when application
51  * launch, this activity is choosen.
52  * 
53  * @author jiri hubacek <jiri.hubacek@gmail.com>
54  * @version %I%, %G%
55  * @since 1.0
56  */
57 public class MainActivity extends ActionBarActivity {
58         public final static Pattern IP_ADDRESS = Pattern.compile("((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))");
59         
60         /**
61          * The {@link android.support.v4.view.PagerAdapter} that will provide
62          * fragments for each of the sections. We use a {@link FragmentPagerAdapter}
63          * derivative, which will keep every loaded fragment in memory. If this
64          * becomes too memory intensive, it may be best to switch to a
65          * {@link android.support.v4.app.FragmentStatePagerAdapter}.
66          */
67         SectionsPagerAdapter mSectionsPagerAdapter;
68
69         /**
70          * The {@link ViewPager} that will host the section contents.
71          */
72         ViewPager mViewPager;
73
74         private WifiManager wifiManager;
75         private WifiLock wifiLock;
76         
77         private Manager orteManager;
78         private DomainApp appDomain;
79         
80         private SettingsDialogFragment settingsDialogFragment = new SettingsDialogFragment();
81         private HelpDialogFragment helpDialogFragment = new HelpDialogFragment();
82
83         /**
84          * Setting up workspace.
85          * 
86          * @param savedInstanceState
87          * @since 1.0
88          */
89         @Override
90         protected void onCreate(Bundle savedInstanceState) {
91                 super.onCreate(savedInstanceState);
92                 setContentView(R.layout.activity_main);
93
94                 // Create the adapter that will return a fragment for each of the three
95                 // primary sections of the activity.
96                 mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
97
98                 // Set up the ViewPager with the sections adapter.
99                 mViewPager = (ViewPager) findViewById(R.id.pager);
100                 mViewPager.setAdapter(mSectionsPagerAdapter);
101                 
102                 // From Robot_Demo project.
103                 wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
104                 wifiLock = (WifiLock) wifiManager.createWifiLock((
105                                         (android.os.Build.VERSION.SDK_INT >= 12)
106                                         ? WifiManager.WIFI_MODE_FULL_HIGH_PERF
107                                         : WifiManager.WIFI_MODE_FULL
108                                 ), getClass().getName());
109                 
110                                 
111                 // Start ORTE.
112                 orteManager = new Manager("");
113                 appDomain = new DomainApp();
114                 
115         }
116
117         /**
118          * When pause activity, release WiFi lock.
119          * 
120          * @since 1.0
121          */
122         @Override
123         public void onPause()
124         {
125                 super.onPause();
126                 
127                 wifiLock.release();
128         }
129         
130         /**
131          * When resume activity, acquire WiFi lock.
132          * 
133          * @since 1.0
134          */
135         @Override
136         public void onResume()
137         {
138                 super.onResume();
139                 
140                 wifiLock.acquire();
141         }
142         
143         /**
144          * When switching application off.
145          * 
146          * @since 1.0
147          */
148         @Override
149         public void onDestroy()
150         {
151                 super.onDestroy();
152                 
153                 if (appDomain != null) {
154                         appDomain.destroy();
155                         appDomain = null;
156                 }
157                 if (orteManager != null) {
158                         orteManager.destroy();
159                         orteManager = null;
160                 }
161         }
162         
163         /**
164          * Adding elements to menu.
165          * 
166          * @since 1.0
167          */
168         @Override
169         public boolean onCreateOptionsMenu(Menu menu) {
170                 // Inflate the menu; this adds items to the action bar if it is present.
171                 getMenuInflater().inflate(R.menu.main, menu);
172                 return true;
173         }
174
175         /**
176          * When Settings or Help button is selected.
177          * 
178          * @since 1.0
179          */
180         @Override
181         public boolean onOptionsItemSelected(MenuItem item) {
182                 // Handle action bar item clicks here. The action bar will
183                 // automatically handle clicks on the Home/Up button, so long
184                 // as you specify a parent activity in AndroidManifest.xml.
185                 int id = item.getItemId();
186                 switch (id) {
187                 case R.id.action_settings:
188                         //TODO Menu Settings (prefer own activity)
189                         // Ideas about settings:
190                         // - input for manager's ip adresses
191                         // - enable/disable scaling, destination screen size
192                         
193                         settingsDialogFragment.show(getSupportFragmentManager(), "dialog");
194                         return true;
195                 case R.id.action_help:
196                         //TODO Menu Help (prefer own activity)
197                         
198                         helpDialogFragment.show(getSupportFragmentManager(), "dialog");
199                         return true;
200                 default:
201                         // Nothing...
202                 }
203                 return super.onOptionsItemSelected(item);
204         }
205
206         /**
207          * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
208          * one of the sections/tabs/pages.
209          */
210         public class SectionsPagerAdapter extends FragmentPagerAdapter {
211
212                 public SectionsPagerAdapter(FragmentManager fm) {
213                         super(fm);
214                 }
215
216                 @Override
217                 public Fragment getItem(int position) {
218                         // getItem is called to instantiate the fragment for the given page.
219                         // Return a PlaceholderFragment (defined as a static inner class
220                         // below).
221                         if (position == 0) {
222                                 PublisherFragment fragment = new PublisherFragment(appDomain);
223                                 fragment.setHasOptionsMenu(true);
224                                 return fragment;
225                         } else {
226                                 SubscriberFragment fragment = new SubscriberFragment(appDomain);
227                                 fragment.setHasOptionsMenu(true);
228                                 return fragment;
229                         }
230                 }
231
232                 @Override
233                 public int getCount() {
234                         // Show 2 total pages - Publisher and Subscriber
235                         return 2;
236                 }
237         }
238         
239         /**
240          * Very easy help/about.
241          * 
242          * @since 1.0
243          */
244         public class HelpDialogFragment extends DialogFragment
245         {
246                 /**
247                  * Create dialog window.
248                  * 
249                  * @since 1.0
250                  */
251                 @Override
252                 public Dialog onCreateDialog(Bundle savedInstanceState)
253                 {
254                         AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
255                         builder.setTitle("Help/About")
256                                 .setMessage("Under construction\n" +
257                                                 "\n" +
258                                                 "written by jiri hubacek\n")
259                                 .setPositiveButton(R.string.dialog_OK, new DialogInterface.OnClickListener() {
260                                         @Override
261                                         public void onClick(DialogInterface dialog, int which)
262                                         {}
263                                 });
264                         
265                         return builder.create();
266                 }
267         }
268         
269         /**
270          * Set up IP addresses of other managers.
271          * 
272          * @since 1.0
273          */
274         public class SettingsDialogFragment extends DialogFragment
275         {
276                 private EditText om;
277                 
278                 /**
279                  * Create dialog window.
280                  * 
281                  * @since 1.0
282                  */
283                 @Override
284                 public Dialog onCreateDialog(Bundle savedInstanceState)
285                 {
286                         // Strings should be done better way, like
287                         // centralized in strings.xml
288                         
289                         AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
290                         builder.setTitle("Your IP is " + Formatter.formatIpAddress(wifiManager.getConnectionInfo().getIpAddress()) + ",\nadd other managers:")
291                                 .setView(text())
292                                 .setPositiveButton(R.string.dialog_OK, new DialogInterface.OnClickListener() {
293                                         
294                                         /**
295                                          * When IP addresses of managers changes,
296                                          * destroy manager and create new one.
297                                          * 
298                                          * @since 1.0
299                                          */
300                                         @Override
301                                         public void onClick(DialogInterface dialog, int which)
302                                         {
303                                                 if (IP_ADDRESS.matcher(om.getText()).matches()) {
304                                                         if (orteManager != null) orteManager.destroy();
305                                                         
306                                                         String managers[] = new String[1];
307                                                         managers[0] = om.getText().toString();
308                                                         
309                                                         orteManager = new Manager(managers);
310                                                 }
311                                         }
312                                 })
313                                 .setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener() {
314                                         
315                                         @Override
316                                         public void onClick(DialogInterface dialog, int which) {
317                                         }
318                                 });
319                         
320                         return builder.create();
321                 }
322                 
323                 /**
324                  * SeekBar for setting-up strength of publishers.
325                  * 
326                  * @return Layout of SeekBar.
327                  * @since 1.0
328                  */
329                 private View text(){
330                         LinearLayout l = new LinearLayout(getActivity());
331                         l.setOrientation(LinearLayout.VERTICAL);
332                         
333                         om = new EditText(getActivity());
334                         om.setInputType(InputType.TYPE_CLASS_PHONE);
335                         om.setText("192.168.1.108");
336                         
337                         l.addView(om);
338                         return l;
339                 }
340         }
341         
342 }