package com.richyan.android.imagegame; import android.content.DialogInterface; import android.content.Intent; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.net.Uri; import android.os.Handler; import android.provider.MediaStore; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import com.google.android.gms.auth.api.Auth; import com.google.android.gms.auth.api.signin.GoogleSignInOptions; import com.google.android.gms.auth.api.signin.GoogleSignInResult; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.common.api.OptionalPendingResult; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Random; import java.util.Timer; import java.util.TimerTask; public class GameActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener{ private int COL =4; private int ROW = 4; private Bitmap[] mImgs; private ImageView[] mImageViews; private int btnHeight; private int noei, noi; private int gi, gj; private int mIVwidth, mIVheight; private int[] ivIndex; final Handler handler = new Handler(); private Timer timer; private TimerTask task; private int mm = 0; private int ss = 0; private boolean start = false; private int lastIVNum; private int imgId = R.drawable.buda; private Bitmap bitmap; private Uri imageUri; private boolean showNumber = false; private EditText timerEtxt; @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.mainmenu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_photo: Intent photoIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(photoIntent, 0); return true; case R.id.action_recommend: Intent send = new Intent(Intent.ACTION_SEND); send.setType("text/plain"); send.putExtra(Intent.EXTRA_TEXT, getResources().getString(R.string.sendMsg)); startActivity(Intent.createChooser(send, getResources().getString(R.string.share))); return true; case R.id.action_about: PopupFragment aboutFragment = new PopupFragment(); aboutFragment.setmPopupId(3); aboutFragment.show(getSupportFragmentManager(),"about"); return true; case R.id.action_camera: Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (takePictureIntent.resolveActivity(getPackageManager()) != null) { startActivityForResult(takePictureIntent,1); } return true; case R.id.action_setting: PopupFragment settingFragment = new PopupFragment(); settingFragment.setmPopupId(1); settingFragment.show(getSupportFragmentManager(),"setting");; return true; case R.id.action_login: Intent signinIntent = new Intent(GameActivity.this, SignInActivity.class); signinIntent.putExtra("imgId", imgId); signinIntent.putExtra("COL",COL); signinIntent.putExtra("showNumber",showNumber); startActivity(signinIntent); finish(); return true; case R.id.action_scores: try { InputStream inputStream = getApplicationContext().openFileInput("scores.txt"); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String receiveString=""; ArrayAdapter records = new ArrayAdapter(this, R.layout.simple_list_green_text); while ( (receiveString=bufferedReader.readLine()) != null ) { String temp = getResources().getString(R.string.player)+ receiveString; temp = temp.replaceFirst("\t","\n"+getResources().getString(R.string.complete)); temp = temp.replaceAll("\t", "\n@: "); records.insert(temp,0); } AlertDialog.Builder builder = new AlertDialog.Builder(GameActivity.this); View view = getLayoutInflater().inflate(R.layout.scores,null); builder.setView(view); ListView lv = (ListView) view.findViewById(R.id.scoresLV); lv.setAdapter(records); builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.show(); inputStream.close(); } catch (FileNotFoundException e) { Toast.makeText(getApplicationContext(),R.string.record,Toast.LENGTH_LONG).show(); return true; } catch (IOException e) {e.printStackTrace(); } return true; default: return super.onOptionsItemSelected(item); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if (requestCode==0 && resultCode == RESULT_OK){ Uri targetUri = data.getData(); //Toast.makeText(getApplicationContext(),targetUri.toString(),Toast.LENGTH_LONG).show(); Intent intent = new Intent(GameActivity.this,GameActivity.class); intent.putExtra("imageUri", targetUri.toString()); intent.putExtra("COL", COL); intent.putExtra("showNumber",showNumber); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); } if (requestCode == 1 && resultCode == RESULT_OK) { Bundle extras = data.getExtras(); Bitmap imageBitmap = (Bitmap) extras.get("data"); Intent intent = new Intent(GameActivity.this, GameActivity.class); intent.putExtra("bitmap", imageBitmap); intent.putExtra("COL", COL); intent.putExtra("showNumber",showNumber); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_game); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); int screenWidth = Resources.getSystem().getDisplayMetrics().widthPixels; int screenHeight = Resources.getSystem().getDisplayMetrics().heightPixels; Bundle bundle = getIntent().getExtras(); if(bundle!=null) { if(bundle.get("bitmap")!=null) bitmap = (Bitmap)bundle.get("bitmap"); else if(bundle.getString("imageUri")!=null) imageUri = Uri.parse(bundle.getString("imageUri")); else { imgId = bundle.getInt("imgId"); } COL = ROW = bundle.getInt("COL"); showNumber = bundle.getBoolean("showNumber"); } if(bitmap !=null){ } else if( imageUri!=null){ try { bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else bitmap = BitmapFactory.decodeResource(getResources(), imgId); btnHeight = COL == 4? 55:20; mIVwidth = (screenWidth)/COL; mIVheight = (screenHeight-btnHeight)/(ROW+2); int imageW = bitmap.getWidth(); int imageH = bitmap.getHeight(); float ratio = mIVheight/mIVwidth; if(imageW>imageH){ bitmap = Bitmap.createBitmap(bitmap,(int)((imageW-imageH/ratio)/2), 0, (int)(imageH/ratio), imageH); }else if(imageW opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient); if(opr.isDone()){ String name= opr.get().getSignInAccount().getDisplayName(); name = name.substring(0,name.indexOf(" ")); nameTxt.setText(name); } } public boolean checkResult(){ for(int i = 0; i<(COL* ROW); i++){ if(ivIndex[i] !=i) return false; } return true; } private void setTimerTask(){ timer = new Timer(); final Handler timerHandler = new Handler(); task = new TimerTask() { @Override public void run() { ss++; if(ss>=60) {mm++; ss=00;} if(mm>=60) {timer.cancel();timer.purge();} timerHandler.post(new Runnable() { @Override public void run() { timerEtxt.setText(((mm<10)?" 0":" ")+ mm+((ss<10)?" :0":" :")+ss ); } }); } }; timer.schedule(task, 1000l, 1000L); } private int getResId(String idName) { String packageName = getPackageName(); int resId = getResources().getIdentifier(idName, "id", packageName); return resId; } public final void shuffleIVs(int emptyIV){ Random rand = new Random(); for(int i=0; i