Android Open Source - miner Play Ground Base






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.screen;
//w ww  .ja  v a  2 s .com
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.Toast;

import com.kolomiyets.miner.Miner;
import com.kolomiyets.miner.R;
import com.kolomiyets.miner.bt.notification.BtListener;
import com.kolomiyets.miner.bt.notification.BtNotificationCmd;
import com.kolomiyets.miner.bt.protocol.CmdFieldState;
import com.kolomiyets.miner.bt.protocol.CmdGameState;
import com.kolomiyets.miner.bt.protocol.CmdGameTeam;
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.ScreenManager.ScreenType;
import com.kolomiyets.miner.view.GridCell;

public abstract class PlayGroundBase extends ScreenBase implements OnClickListener, OnLongClickListener {

  String TAG = PlayGroundBase.class.getSimpleName();
  
  TableLayout grid;  
  volatile ArrayList<GridCell> cells;
  int minesCount;
  int processedCount;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    activity.setCurrentGameState(EGameState.SET);
    cmdReceiver.register(activity);
  }

  @Override
  public void onStart() {
    super.onStart();
    
  };
  
  @Override
  public void onStop() {
    activity.setCurrentGameState(EGameState.TERMINATE);
    super.onStop();
  };
  
  @Override
  public void onDestroy() {
    cmdReceiver.unregister(activity);
    super.onDestroy();
  };
  
  ArrayList<GridCell> generateCells(int dimention){
    ArrayList<GridCell> cells = new ArrayList<GridCell>();
    for(int i=0; i<dimention*dimention; i++){
      GridCell cell = new GridCell(activity);
      cell.setOnClickListener(this);
      cell.setOnLongClickListener(this);
      cells.add(cell);
    }
    return cells;
  }
  
  ArrayList<GridCell> generateCells(List<Integer> mineConfig){
    ArrayList<GridCell> cells = new ArrayList<GridCell>();
    for(Integer v: mineConfig){
      GridCell cell = new GridCell(activity);
      if(v.equals(Integer.valueOf(CmdFieldState.CELL_STATE_MINE))) {
        cell.setMine(true);
        minesCount++;
      } else {
        cell.setMystery();
      }
      cell.setOnClickListener(this);
      cell.setOnLongClickListener(this);
      cells.add(cell);
    }
    return cells;
  }
  
  void generateGrid(TableLayout grid, ArrayList<GridCell> cells, int dimention){
    grid.removeAllViews();
    for(int i=0; i<dimention*dimention; i+=dimention){
      TableRow row = new TableRow(activity);
      row.setFocusable(false);
      row.setClickable(false);
      for(int j=i; j<i+dimention; j++){
        GridCell cell = cells.get(j);
        row.addView(cell, new TableRow.LayoutParams(
            TableRow.LayoutParams.WRAP_CONTENT, 
            TableRow.LayoutParams.WRAP_CONTENT));
      }
      grid.addView(row, new TableLayout.LayoutParams(
          TableLayout.LayoutParams.WRAP_CONTENT, 
          TableLayout.LayoutParams.WRAP_CONTENT));
    }
    grid.setFocusable(false);
    grid.setClickable(false);
    grid.invalidate();
  }
  
  public static float dpToPixel(float dp, Context context){
      Resources resources = context.getResources();
      DisplayMetrics metrics = resources.getDisplayMetrics();
      float px = dp * (metrics.densityDpi / 160f);
      return px;
  }
  
  void setGridEnabled(boolean isEnabled){
    for(GridCell c: cells){
      c.setEnabled(isEnabled);
    }
  }
  
  BtListener<BtNotificationCmd> cmdReceiver = new BtListener<BtNotificationCmd>(BtNotificationCmd.class) {
    
    @Override
    public void processNotification(BtNotificationCmd notification) {
      
      if(notification.cmd instanceof CmdGameState){
        if(((CmdGameState)notification.cmd).state.equals(EGameState.TERMINATE)){
          this.unregister(activity);
          DialogProvider.showDialog(activity, EDialogType.INFO, 
              getResources().getString(R.string.dlg_ttl_err), 
              getResources().getString(R.string.dlg_msg_op_abandoned), 
              new IDialogResult() {
                
                @Override
                public void onResult(DialogFragment dlg, int result) {
                  activity.getConnectionManager().terminateConnection();
                  activity.getScreenManager().goTo(ScreenType.START);
                }
              });
        }
      } else if(notification.cmd instanceof CmdFieldState) {
        processIncommingFieldState(((CmdFieldState)notification.cmd).stateMatrix);
      }
    }
  };
  
  void anounceWinner(boolean isMiner){
    DialogProvider.showDialog(activity, EDialogType.INFO, 
        getResources().getString(R.string.dlg_ttl_finish), 
        getResources().getString(isMiner?R.string.dlg_msg_miner_wins:R.string.dlg_msg_sapper_wins), null);
  }
  
  abstract void processIncommingFieldState(ArrayList<Integer> stateMatrix);
  abstract void minerWins();
  abstract void sapperWins();
  abstract void finalizeConnection();
  abstract ArrayList<Integer> generateStateMatrix();
}




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