3 * This file is part of shape4a.
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.
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.
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/>.
19 package org.ocera.orte.shape4a;
21 import java.util.regex.Pattern;
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;
44 import org.ocera.orte.DomainApp;
45 import org.ocera.orte.Manager;
47 //TODO Better navigation (swipe sometimes miss drag).
50 * Main application activity, when application
51 * launch, this activity is choosen.
53 * @author jiri hubacek <jiri.hubacek@gmail.com>
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]?))");
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}.
67 SectionsPagerAdapter mSectionsPagerAdapter;
70 * The {@link ViewPager} that will host the section contents.
74 private WifiManager wifiManager;
75 private WifiLock wifiLock;
77 private Manager orteManager;
78 private DomainApp appDomain;
80 private SettingsDialogFragment settingsDialogFragment = new SettingsDialogFragment();
81 private HelpDialogFragment helpDialogFragment = new HelpDialogFragment();
84 * Setting up workspace.
86 * @param savedInstanceState
90 protected void onCreate(Bundle savedInstanceState) {
91 super.onCreate(savedInstanceState);
92 setContentView(R.layout.activity_main);
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());
98 // Set up the ViewPager with the sections adapter.
99 mViewPager = (ViewPager) findViewById(R.id.pager);
100 mViewPager.setAdapter(mSectionsPagerAdapter);
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());
112 orteManager = new Manager("");
113 appDomain = new DomainApp();
118 * When pause activity, release WiFi lock.
123 public void onPause()
131 * When resume activity, acquire WiFi lock.
136 public void onResume()
144 * When switching application off.
149 public void onDestroy()
153 if (appDomain != null) {
157 if (orteManager != null) {
158 orteManager.destroy();
164 * Adding elements to menu.
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);
176 * When Settings or Help button is selected.
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();
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
193 settingsDialogFragment.show(getSupportFragmentManager(), "dialog");
195 case R.id.action_help:
196 //TODO Menu Help (prefer own activity)
198 helpDialogFragment.show(getSupportFragmentManager(), "dialog");
203 return super.onOptionsItemSelected(item);
207 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
208 * one of the sections/tabs/pages.
210 public class SectionsPagerAdapter extends FragmentPagerAdapter {
212 public SectionsPagerAdapter(FragmentManager fm) {
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
222 PublisherFragment fragment = new PublisherFragment(appDomain);
223 fragment.setHasOptionsMenu(true);
226 SubscriberFragment fragment = new SubscriberFragment(appDomain);
227 fragment.setHasOptionsMenu(true);
233 public int getCount() {
234 // Show 2 total pages - Publisher and Subscriber
240 * Very easy help/about.
244 public class HelpDialogFragment extends DialogFragment
247 * Create dialog window.
252 public Dialog onCreateDialog(Bundle savedInstanceState)
254 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
255 builder.setTitle("Help/About")
256 .setMessage("Under construction\n" +
258 "written by jiri hubacek\n")
259 .setPositiveButton(R.string.dialog_OK, new DialogInterface.OnClickListener() {
261 public void onClick(DialogInterface dialog, int which)
265 return builder.create();
270 * Set up IP addresses of other managers.
274 public class SettingsDialogFragment extends DialogFragment
279 * Create dialog window.
284 public Dialog onCreateDialog(Bundle savedInstanceState)
286 // Strings should be done better way, like
287 // centralized in strings.xml
289 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
290 builder.setTitle("Your IP is " + Formatter.formatIpAddress(wifiManager.getConnectionInfo().getIpAddress()) + ",\nadd other managers:")
292 .setPositiveButton(R.string.dialog_OK, new DialogInterface.OnClickListener() {
295 * When IP addresses of managers changes,
296 * destroy manager and create new one.
301 public void onClick(DialogInterface dialog, int which)
303 if (IP_ADDRESS.matcher(om.getText()).matches()) {
304 if (orteManager != null) orteManager.destroy();
306 String managers[] = new String[1];
307 managers[0] = om.getText().toString();
309 orteManager = new Manager(managers);
313 .setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener() {
316 public void onClick(DialogInterface dialog, int which) {
320 return builder.create();
324 * SeekBar for setting-up strength of publishers.
326 * @return Layout of SeekBar.
330 LinearLayout l = new LinearLayout(getActivity());
331 l.setOrientation(LinearLayout.VERTICAL);
333 om = new EditText(getActivity());
334 om.setInputType(InputType.TYPE_CLASS_PHONE);
335 om.setText("192.168.1.108");