Android Open Source - miner Miner Activity






From Project

Back to project page miner.

License

The source code is released under:

Apache License

If you think the Android project miner listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.kolomiyets.miner;
/*from   w ww. ja  v  a 2s.  c o  m*/
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;

import com.kolomiyets.miner.bt.BtStateMonitor;
import com.kolomiyets.miner.bt.ConnectionBase;
import com.kolomiyets.miner.bt.ConnectionManager;
import com.kolomiyets.miner.bt.EConnectMethod;
import com.kolomiyets.miner.bt.notification.EConnectionSate;
import com.kolomiyets.miner.bt.protocol.CmdGameState;
import com.kolomiyets.miner.bt.protocol.EGameState;
import com.kolomiyets.miner.dialog.DialogProvider;
import com.kolomiyets.miner.dialog.EDialogType;
import com.kolomiyets.miner.dialog.IDialogResult;
import com.kolomiyets.miner.screen.PlayGroundBase;
import com.kolomiyets.miner.screen.ScreenBase;
import com.kolomiyets.miner.screen.ScreenConnectingGame;
import com.kolomiyets.miner.screen.ScreenDevices;
import com.kolomiyets.miner.screen.ScreenManager;
import com.kolomiyets.miner.screen.ScreenManager.ScreenType;
import com.kolomiyets.miner.screen.ScreenSelectPlayer;

public class MinerActivity extends FragmentActivity {

  public final static int REQUEST_ENABLE_BT = 1001;
  public final static int REQUEST_VISIBLE_BT = 1002;
  
  private ScreenManager screenManager;
  private ConnectionManager connectionManager;
  private BtStateMonitor btStateMonitor;
  private GameStatePoster gameStatePoster;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.miner_activity_layout);
    
    connectionManager = new ConnectionManager(this);
    screenManager = new ScreenManager(getSupportFragmentManager(), R.id.screen_container);
    btStateMonitor = new BtStateMonitor(this);
  }
  
  @Override
  protected void onStart() {
    super.onStart();
    btStateMonitor.startMonitoring();
  }
  
  @Override
  protected void onStop() {
    stopGameStatePoster();
    btStateMonitor.stopMonitoring();
    connectionManager.terminateConnection();
    super.onStop();
  }
  
  @Override
  protected void onResume() {
    super.onResume();
    if(!connectionManager.isBlueToothAvailable()){
      DialogProvider.showDialog(this, EDialogType.INFO, 
          getString(R.string.dlg_ttl_err), getString(R.string.dlg_msg_no_blue_tooth), 
          new IDialogResult() {
            
            @Override
            public void onResult(DialogFragment dlg, int result) {
              finish();
            }
          });
    }
  }
  
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
    ScreenBase curScr = screenManager.getCurrentScreen();
    if(requestCode==REQUEST_ENABLE_BT
        && curScr instanceof ScreenConnectingGame){
      
      ((ScreenConnectingGame)curScr).onRequestEnableResult(resultCode);
    } else if(requestCode==REQUEST_VISIBLE_BT
        && curScr instanceof ScreenConnectingGame) {
      
      ((ScreenConnectingGame)curScr).onRequestVisibilityResult(resultCode);
    }
    super.onActivityResult(requestCode, resultCode, data);
  }
  
  public ScreenManager getScreenManager() {
    return screenManager;
  }
  
  public synchronized ConnectionManager getConnectionManager() {
    return connectionManager;
  }
  
  public synchronized EGameState getCurrentGameState(){
    return ((Miner)getApplicationContext()).getCurrentGameState();
  }
  
  public synchronized void setCurrentGameState(EGameState newState){
    ((Miner)getApplicationContext()).setCurrentGameState(newState);
  }
  
  public void startGameStatePoster(){
    gameStatePoster = new GameStatePoster();
    gameStatePoster.start();
  }
  
  public void stopGameStatePoster(){
    ConnectionBase.killThread(gameStatePoster);
  }
  
  private class GameStatePoster extends Thread {
    @Override
    public void run() {
      while(!this.isInterrupted()
          && !getCurrentGameState().equals(EGameState.TERMINATE)
          && !getCurrentGameState().equals(EGameState.FINISH)
          && !getCurrentGameState().equals(EGameState.IDLE)){
        try {
          getConnectionManager().sendCmd(new CmdGameState(getCurrentGameState()));
          Thread.sleep(200);
        } catch (Exception e) {
          e.printStackTrace();
          break;
        }
      }
    }
    
  }
  
  @Override
  public void onBackPressed() {
    ScreenBase scr = screenManager.getCurrentScreen();
    if(scr instanceof PlayGroundBase
        || scr instanceof ScreenSelectPlayer){
      connectionManager.terminateConnection();
      screenManager.goTo(ScreenType.START);
    } else if (scr instanceof ScreenConnectingGame
        || scr instanceof ScreenDevices){
      connectionManager.terminateConnection();
      screenManager.previous();
    } else if(scr instanceof PlayGroundBase){
      connectionManager.terminateConnection();
      screenManager.goTo(ScreenType.START);
    } else {
      super.onBackPressed();
    }
  }
  
}




Java Source Code List

com.kolomiyets.miner.MinerActivity.java
com.kolomiyets.miner.Miner.java
com.kolomiyets.miner.bt.BtStateMonitor.java
com.kolomiyets.miner.bt.ConnectionBase.java
com.kolomiyets.miner.bt.ConnectionManager.java
com.kolomiyets.miner.bt.ConnectionMaster.java
com.kolomiyets.miner.bt.ConnectionProcessor.java
com.kolomiyets.miner.bt.ConnectionSlave.java
com.kolomiyets.miner.bt.EConnectMethod.java
com.kolomiyets.miner.bt.IBtEnableCallback.java
com.kolomiyets.miner.bt.IRequestBtResult.java
com.kolomiyets.miner.bt.notification.BtListener.java
com.kolomiyets.miner.bt.notification.BtNotificationCmd.java
com.kolomiyets.miner.bt.notification.BtNotificationConnect.java
com.kolomiyets.miner.bt.notification.BtNotificationDiscoveryDevice.java
com.kolomiyets.miner.bt.notification.BtNotificationDiscoveryState.java
com.kolomiyets.miner.bt.notification.BtNotificationDiscovery.java
com.kolomiyets.miner.bt.notification.BtNotificationHandshake.java
com.kolomiyets.miner.bt.notification.BtNotificationPower.java
com.kolomiyets.miner.bt.notification.BtNotificationState.java
com.kolomiyets.miner.bt.notification.BtNotification.java
com.kolomiyets.miner.bt.notification.EConnectionSate.java
com.kolomiyets.miner.bt.notification.NotificationManager.java
com.kolomiyets.miner.bt.protocol.CmdBase.java
com.kolomiyets.miner.bt.protocol.CmdFactory.java
com.kolomiyets.miner.bt.protocol.CmdFieldState.java
com.kolomiyets.miner.bt.protocol.CmdGameState.java
com.kolomiyets.miner.bt.protocol.CmdGameTeam.java
com.kolomiyets.miner.bt.protocol.CmdHandshake.java
com.kolomiyets.miner.bt.protocol.CmdPing.java
com.kolomiyets.miner.bt.protocol.CmdResponse.java
com.kolomiyets.miner.bt.protocol.ECmdName.java
com.kolomiyets.miner.bt.protocol.EGameState.java
com.kolomiyets.miner.dialog.DialogProvider.java
com.kolomiyets.miner.dialog.EDialogType.java
com.kolomiyets.miner.dialog.IDialogResult.java
com.kolomiyets.miner.screen.PlayGroundBase.java
com.kolomiyets.miner.screen.PlayGroundMiner.java
com.kolomiyets.miner.screen.PlayGroundSapper.java
com.kolomiyets.miner.screen.ScreenBase.java
com.kolomiyets.miner.screen.ScreenConnectingGame.java
com.kolomiyets.miner.screen.ScreenCreateGame.java
com.kolomiyets.miner.screen.ScreenDevices.java
com.kolomiyets.miner.screen.ScreenManager.java
com.kolomiyets.miner.screen.ScreenSelectPlayer.java
com.kolomiyets.miner.view.GridCell.java