Pavle_nis

MainActivity

Feb 5th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.52 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity
  2. {
  3.     private boolean isCableConnected = false;
  4.     private boolean cableStateChanged = false;
  5.     private boolean popupWasShown = false;
  6.  
  7.     public static final String IS_CONNECTED_KEY = "isConnectedValue";
  8.  
  9.     public void startOtgService()
  10.     {
  11.         startService(new Intent(MainActivity.this, OtgService.class));
  12.  
  13.     }
  14.  
  15.     public void stopOtgService()
  16.     {
  17.         stopService(new Intent(MainActivity.this, OtgService.class));
  18.  
  19.     }
  20.  
  21.     @Override
  22.     protected void onCreate(Bundle savedInstanceState)
  23.     {
  24.         super.onCreate(savedInstanceState);
  25.         setContentView(R.layout.activity_main);
  26.         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  27.         setSupportActionBar(toolbar);
  28.  
  29.         FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  30.         fab.setOnClickListener(new View.OnClickListener()
  31.         {
  32.             @Override
  33.             public void onClick(View view)
  34.             {
  35.                 Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
  36.                         .setAction("Action", null).show();
  37.             }
  38.         });
  39.  
  40.         Button startButton = (Button)this.findViewById(R.id.startButton);
  41.         Button stopButton = (Button)this.findViewById(R.id.stopButton);
  42.         //startAlarm();
  43.  
  44.         startButton.setOnClickListener(new View.OnClickListener()
  45.         {
  46.             @Override
  47.             public void onClick(View v)
  48.             {
  49.                 startOtgService();
  50.             }
  51.         });
  52.  
  53.         stopButton.setOnClickListener(new View.OnClickListener()
  54.         {
  55.             @Override
  56.             public void onClick(View v)
  57.             {
  58.                 stopOtgService();
  59.             }
  60.         });
  61.     }
  62.  
  63.     @Override
  64.     public boolean onCreateOptionsMenu(Menu menu)
  65.     {
  66.         // Inflate the menu; this adds items to the action bar if it is present.
  67.         getMenuInflater().inflate(R.menu.menu_main, menu);
  68.  
  69.         //menu.findItem(R.id.action_cable).setVisible(false);
  70.  
  71.         return true;
  72.     }
  73.  
  74.     @Override
  75.     public boolean onPrepareOptionsMenu(Menu menu){
  76.         MenuItem item = menu.findItem(R.id.action_cable);
  77.         if (cableStateChanged && !popupWasShown) {
  78.             item.setVisible(true);
  79.             cableStateChanged = false;
  80.         } else if (popupWasShown) {
  81.             item.setVisible(false);
  82.         }
  83.         return super.onPrepareOptionsMenu(menu);
  84.     }
  85.  
  86.     @Override
  87.     public boolean onOptionsItemSelected(MenuItem item)
  88.     {
  89.         int id = item.getItemId();
  90.  
  91.         //noinspection SimplifiableIfStatement
  92.         if (id == R.id.action_settings)
  93.         {
  94.             return true;
  95.         }
  96.         else if(id ==  R.id.action_cable)
  97.         {
  98.             popupWasShown = true;
  99.             invalidateOptionsMenu();
  100.             // You can change the popup title, message and buttons here
  101.             AlertDialog.Builder builder = new AlertDialog.Builder(this);
  102.             builder.setTitle(isCableConnected ? "Cable was connected" : "Cable was disconnected");
  103.             builder.setMessage("foo bar");
  104.             builder.setPositiveButton("OK", null);
  105.             builder.setNegativeButton(null, null);
  106.             builder.create().show();
  107.         }
  108.         return super.onOptionsItemSelected(item);
  109.     }
  110.  
  111.     private void cableStateWasChanged(boolean isConnected)
  112.     {
  113.         isCableConnected = isConnected;
  114.         cableStateChanged = true;
  115.         popupWasShown = false;
  116.         invalidateOptionsMenu();
  117.     }
  118.  
  119.     @Override
  120.     protected void onNewIntent(Intent intent)
  121.     {
  122.         if (intent.getExtras() == null)
  123.         {
  124.             super.onNewIntent(intent);
  125.             Log.e("###", "No extras");
  126.             return;
  127.         }
  128.  
  129.         if (intent.hasExtra(IS_CONNECTED_KEY))
  130.         {
  131.             Log.e("###", "Displaying dialog");
  132.  
  133.             boolean isConnected = intent.getExtras().getBoolean(IS_CONNECTED_KEY);
  134.  
  135.             final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
  136.             alertDialog.setTitle("Otg state changed");
  137.  
  138.             if (isConnected)
  139.                 alertDialog.setMessage("OTG connected");
  140.             else
  141.                 alertDialog.setMessage("OTG disconnected");
  142.  
  143.             //alertDialog.setIcon(R.drawable.icon);
  144.  
  145.             alertDialog.show();
  146.         }
  147.         else
  148.         {
  149.             Log.e("###", "Does not contain key");
  150.             super.onNewIntent(intent);
  151.         }
  152.     }
  153. }
Add Comment
Please, Sign In to add comment