2 * SaveMySugar - Exchange messages using the distance between phone calls
4 * Copyright (C) 2015 Antonio Ospite <ao2@ao2.it>
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.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 package it.ao2.savemysugar;
22 import android.annotation.SuppressLint;
23 import android.content.DialogInterface;
24 import android.content.Intent;
25 import android.os.Bundle;
26 import android.support.design.widget.TabLayout;
27 import android.support.v4.app.Fragment;
28 import android.support.v4.app.FragmentManager;
29 import android.support.v4.app.FragmentPagerAdapter;
30 import android.support.v4.view.ViewPager;
31 import android.support.v7.app.AlertDialog;
32 import android.support.v7.app.AppCompatActivity;
33 import android.support.v7.widget.Toolbar;
34 import android.view.KeyEvent;
35 import android.view.LayoutInflater;
36 import android.view.Menu;
37 import android.view.MenuItem;
38 import android.view.View;
39 import android.widget.TextView;
41 import java.util.ArrayList;
42 import java.util.List;
44 public class MainActivity extends AppCompatActivity {
45 private static final String TAG = "SaveMySugar";
46 private static final int REQUEST_PREFS = 1;
48 /** Called when the activity is first created. */
50 public void onCreate(Bundle savedInstanceState) {
51 super.onCreate(savedInstanceState);
52 setContentView(R.layout.activity_main);
54 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
55 setSupportActionBar(toolbar);
57 ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
58 adapter.addFragment(new ReceiveFragment(), "Receive");
59 adapter.addFragment(new SendFragment(), "Send");
61 ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
62 viewPager.setAdapter(adapter);
64 TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
65 tabLayout.setupWithViewPager(viewPager);
68 static class ViewPagerAdapter extends FragmentPagerAdapter {
69 private final List<Fragment> mFragments = new ArrayList<>();
70 private final List<String> mFragmentTitles = new ArrayList<>();
72 ViewPagerAdapter(FragmentManager fm) {
76 public void addFragment(Fragment fragment, String title) {
77 mFragments.add(fragment);
78 mFragmentTitles.add(title);
82 public Fragment getItem(int position) {
83 return mFragments.get(position);
87 public int getCount() {
88 return mFragments.size();
92 public CharSequence getPageTitle(int position) {
93 return mFragmentTitles.get(position);
97 // Workaround to have the generic menu key (either a hardware one or the
98 // one the in bottom navigation bar) open the overflow menu.
100 public boolean onKeyUp(int keyCode, KeyEvent event) {
101 if (keyCode == KeyEvent.KEYCODE_MENU) {
102 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
103 if (toolbar.isOverflowMenuShowing()) {
104 toolbar.dismissPopupMenus();
106 toolbar.showOverflowMenu();
110 return super.onKeyUp(keyCode, event);
114 public boolean onCreateOptionsMenu(Menu menu) {
115 getMenuInflater().inflate(R.menu.main, menu);
120 public boolean onOptionsItemSelected(MenuItem item) {
121 switch (item.getItemId()) {
123 case R.id.menu_settings:
124 Intent i = new Intent(this, SettingsActivity.class);
125 startActivityForResult(i, REQUEST_PREFS);
128 case R.id.action_about:
129 @SuppressLint("InflateParams")
130 // See the last section in
131 // https://possiblemobile.com/2013/05/layout-inflation-as-intended/
132 View view = LayoutInflater.from(this).inflate(R.layout.about, null);
134 ((TextView) view.findViewById(R.id.version)).setText(BuildConfig.VERSION_NAME);
136 AlertDialog alrt = new AlertDialog.Builder(this).setView(view).create();
137 alrt.setTitle(R.string.about_title);
138 alrt.setButton(AlertDialog.BUTTON_NEGATIVE, getString(R.string.ok),
139 new DialogInterface.OnClickListener() {
141 public void onClick(DialogInterface dialog, int whichButton) {
148 return super.onOptionsItemSelected(item);
152 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
153 super.onActivityResult(requestCode, resultCode, data);
155 switch (requestCode) {
157 ((SaveMySugarApplication) getApplication()).updateModulatorParams();