Advertisement
minafaw3

MainActivity AIDMAID

Feb 9th, 2016
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.31 KB | None | 0 0
  1. package org.aidmaid;
  2.  
  3. import android.app.ActivityManager;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.net.Uri;
  7. import android.os.Bundle;
  8. import android.support.design.widget.FloatingActionButton;
  9. import android.support.v4.app.Fragment;
  10. import android.support.v4.app.FragmentManager;
  11. import android.support.v4.app.FragmentPagerAdapter;
  12. import android.support.v4.view.ViewPager;
  13. import android.support.v7.app.AppCompatActivity;
  14. import android.view.View;
  15.  
  16. import com.facebook.CallbackManager;
  17. import com.facebook.FacebookSdk;
  18.  
  19. import org.aidmaid.alarm.AlarmService;
  20. import org.aidmaid.broadcast.Red5TranslationViewActivity;
  21. import org.aidmaid.broadcast.RemoteVideoStreamConfig;
  22. import org.aidmaid.discover.DiscoverFragment;
  23. import org.aidmaid.pageIndicator.CirclePageIndicator;
  24. import org.aidmaid.settings.ApplicationSettings;
  25. import org.aidmaid.shaking.ShakeDetectionService;
  26. import org.aidmaid.wear.WearListenerService;
  27.  
  28. import java.util.Locale;
  29.  
  30. public class MainActivity extends AppCompatActivity {
  31.  
  32. /**
  33. * The {@link android.support.v4.view.PagerAdapter} that will provide
  34. * fragments for each of the sections. We use a
  35. * {@link FragmentPagerAdapter} derivative, which will keep every
  36. * loaded fragment in memory. If this becomes too memory intensive, it
  37. * may be best to switch to a
  38. * {@link android.support.v4.app.FragmentStatePagerAdapter}.
  39. */
  40. SectionsPagerAdapter mSectionsPagerAdapter;
  41.  
  42. /**
  43. * The {@link ViewPager} that will host the section contents.
  44. */
  45. ViewPager mViewPager;
  46. //MaterialTabHost tabHost;
  47. private int tabId;
  48. FloatingActionButton settingsButton;
  49.  
  50. @Override
  51. protected void onCreate(Bundle savedInstanceState) {
  52.  
  53. super.onCreate(savedInstanceState);
  54. FacebookSdk.sdkInitialize(getApplicationContext());
  55. setContentView(R.layout.activity_main);
  56. initialize();
  57.  
  58.  
  59.  
  60. //PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
  61. //tabs.setViewPager(mViewPager);
  62. //tabs.setIndicatorColor(0xFFFFFFFF);
  63. //tabs.setUnderlineColor(0xFFFFA0A0);
  64. //tabs.setBackgroundColor(0xFFd0021b);
  65. //tabs.setTextColor(0xFFFFFFFF);
  66. //tabs.setIndicatorHeight(6);
  67.  
  68. CirclePageIndicator circlePageIndicator = (CirclePageIndicator) findViewById(R.id.circlePageIndicator);
  69. circlePageIndicator.setViewPager(mViewPager);
  70. circlePageIndicator.setCurrentItem(0);
  71. circlePageIndicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
  72. @Override
  73. public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  74. if (position == 2) {
  75. settingsButton.setVisibility(View.GONE);
  76. }
  77. }
  78.  
  79. @Override
  80. public void onPageSelected(int position) {
  81.  
  82. }
  83.  
  84. @Override
  85. public void onPageScrollStateChanged(int state) {
  86.  
  87. }
  88. });
  89.  
  90.  
  91. }
  92.  
  93. @Override
  94. public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
  95. super.onActivityResult(requestCode, resultCode, data);
  96. CallbackManager callbackManager = CallbackManager.Factory.create();
  97. callbackManager.onActivityResult(requestCode, resultCode, data);
  98. if (FacebookSdk.isFacebookRequestCode(requestCode) && mViewPager.getCurrentItem() == 0) {
  99. Intent intent = new Intent(Intent.ACTION_DIAL);
  100. intent.setData(Uri.parse("tel:" + ApplicationSettings.getPolicePhone(getApplicationContext())));
  101. startActivity(intent);
  102. }
  103.  
  104. }
  105.  
  106. private void initialize() {
  107. // Create the adapter that will return a fragment for each of the three
  108. // primary sections of the activity.
  109. mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
  110.  
  111. // Set up the ViewPager with the sections adapter.
  112. mViewPager = (ViewPager) findViewById(R.id.pager);
  113. mViewPager.setAdapter(mSectionsPagerAdapter);
  114.  
  115. settingsButton = (FloatingActionButton) findViewById(R.id.settingsImageButton);
  116. settingsButton.setOnClickListener(new View.OnClickListener() {
  117. public void onClick(View v) {
  118. mViewPager.setCurrentItem(2);
  119. }
  120. });
  121.  
  122.  
  123. Intent shakeDetectionIntent = new Intent(this, ShakeDetectionService.class);
  124. Intent alarmServiceIntent = new Intent(this, AlarmService.class);
  125. Intent wearListenerServiceIntent = new Intent(this, WearListenerService.class);
  126.  
  127. boolean hasStartedShakeDetection = false;
  128. boolean hasStartedAlarmService = false;
  129. boolean hasStartedWearService = false;
  130.  
  131. ActivityManager manager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
  132. for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
  133. if (ShakeDetectionService.class.getName().equals(service.service.getClassName())) {
  134. hasStartedShakeDetection = true;
  135. }
  136. if (AlarmService.class.getName().equals(service.service.getClassName())) {
  137. hasStartedAlarmService = true;
  138. }
  139.  
  140. if (WearListenerService.class.getName().equals(service.service.getClassName())) {
  141. hasStartedWearService = true;
  142. }
  143. }
  144.  
  145. if (!hasStartedAlarmService)
  146. startService(alarmServiceIntent);
  147.  
  148. if (!hasStartedShakeDetection)
  149. startService(shakeDetectionIntent);
  150.  
  151. if (!hasStartedWearService)
  152. startService(wearListenerServiceIntent);
  153.  
  154. Uri data = getIntent().getData();
  155.  
  156. mViewPager.setCurrentItem(1);
  157. if (data != null && data.getScheme().equals("aidmaidlogin")) {
  158. mViewPager.setCurrentItem(4);
  159. }
  160.  
  161. if (data != null && data.getScheme().equals("aidmaid")) {
  162. Intent startBroadcast = new Intent(this, Red5TranslationViewActivity.class);
  163. startBroadcast.putExtra(RemoteVideoStreamConfig.VIDEO_STREAM_PARAMETER_NAME, data.getHost());
  164. startActivity(startBroadcast);
  165. }
  166. }
  167.  
  168. /**
  169. * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
  170. * one of the sections/tabs/pages.
  171. */
  172. public class SectionsPagerAdapter extends FragmentPagerAdapter {
  173.  
  174. public SectionsPagerAdapter(FragmentManager fm) {
  175. super(fm);
  176. }
  177.  
  178. @Override
  179. public Fragment getItem(int position) {
  180.  
  181. if (position == 0)
  182. return DiscoverFragment.newInstance();
  183. if (position == 1)
  184. return PanicButtonFragment.newInstance();
  185. if (position == 2)
  186. return SettingsFragment.newInstance();
  187. //return CrimesMapFragment.newInstance();
  188. // if(position == 4)
  189.  
  190.  
  191. return null;
  192. // getItem is called to instantiate the fragment for the given page.
  193. // Return a PlaceholderFragment (defined as a static inner class below).
  194. //return SliderFragment.newInstance();
  195.  
  196. }
  197.  
  198. @Override
  199. public int getCount() {
  200. // Show 3 total pages.
  201. return 3;
  202. }
  203.  
  204. @Override
  205. public CharSequence getPageTitle(int position) {
  206. Locale l = Locale.getDefault();
  207. switch (position) {
  208. case 0:
  209. return getString(R.string.title_section1).toUpperCase(l);
  210. // return getString(R.string.title_section3).toUpperCase(l);
  211. case 1:
  212. return getString(R.string.title_section2).toUpperCase(l);
  213. //return getString(R.string.title_section1).toUpperCase(l);
  214. case 2:
  215. return getString(R.string.action_settings).toUpperCase(l);
  216. // return getString(R.string.title_section2).toUpperCase(l);
  217. // case 3:
  218. // return getString(R.string.title_section4).toUpperCase(l);
  219. // case 4:
  220. // return getString(R.string.title_section5).toUpperCase(l);
  221. }
  222. return null;
  223. }
  224. }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement