Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends AppCompatActivity
- {
- private boolean isCableConnected = false;
- private boolean cableStateChanged = false;
- private boolean popupWasShown = false;
- public static final String IS_CONNECTED_KEY = "isConnectedValue";
- public void startOtgService()
- {
- startService(new Intent(MainActivity.this, OtgService.class));
- }
- public void stopOtgService()
- {
- stopService(new Intent(MainActivity.this, OtgService.class));
- }
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
- fab.setOnClickListener(new View.OnClickListener()
- {
- @Override
- public void onClick(View view)
- {
- Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
- .setAction("Action", null).show();
- }
- });
- Button startButton = (Button)this.findViewById(R.id.startButton);
- Button stopButton = (Button)this.findViewById(R.id.stopButton);
- //startAlarm();
- startButton.setOnClickListener(new View.OnClickListener()
- {
- @Override
- public void onClick(View v)
- {
- startOtgService();
- }
- });
- stopButton.setOnClickListener(new View.OnClickListener()
- {
- @Override
- public void onClick(View v)
- {
- stopOtgService();
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu)
- {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- //menu.findItem(R.id.action_cable).setVisible(false);
- return true;
- }
- @Override
- public boolean onPrepareOptionsMenu(Menu menu){
- MenuItem item = menu.findItem(R.id.action_cable);
- if (cableStateChanged && !popupWasShown) {
- item.setVisible(true);
- cableStateChanged = false;
- } else if (popupWasShown) {
- item.setVisible(false);
- }
- return super.onPrepareOptionsMenu(menu);
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item)
- {
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings)
- {
- return true;
- }
- else if(id == R.id.action_cable)
- {
- popupWasShown = true;
- invalidateOptionsMenu();
- // You can change the popup title, message and buttons here
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle(isCableConnected ? "Cable was connected" : "Cable was disconnected");
- builder.setMessage("foo bar");
- builder.setPositiveButton("OK", null);
- builder.setNegativeButton(null, null);
- builder.create().show();
- }
- return super.onOptionsItemSelected(item);
- }
- private void cableStateWasChanged(boolean isConnected)
- {
- isCableConnected = isConnected;
- cableStateChanged = true;
- popupWasShown = false;
- invalidateOptionsMenu();
- }
- @Override
- protected void onNewIntent(Intent intent)
- {
- if (intent.getExtras() == null)
- {
- super.onNewIntent(intent);
- Log.e("###", "No extras");
- return;
- }
- if (intent.hasExtra(IS_CONNECTED_KEY))
- {
- Log.e("###", "Displaying dialog");
- boolean isConnected = intent.getExtras().getBoolean(IS_CONNECTED_KEY);
- final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
- alertDialog.setTitle("Otg state changed");
- if (isConnected)
- alertDialog.setMessage("OTG connected");
- else
- alertDialog.setMessage("OTG disconnected");
- //alertDialog.setIcon(R.drawable.icon);
- alertDialog.show();
- }
- else
- {
- Log.e("###", "Does not contain key");
- super.onNewIntent(intent);
- }
- }
- }
Add Comment
Please, Sign In to add comment