Can not add SeekBar to MediaPlayer. - android-mediaplayer

I have tried Runnable as well but of no use. Here is my code, What can be done here?
I have used Parecelable method to get my currently selected song from main activity to this playscreen activity.Now I want to attach seekbar to that 'currsong' I received when my mediaplayer mPlayer starts
package com.musicplayer;
import android.content.ContentUris;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.PowerManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.util.Random;
public class PlayScreen extends ActionBarActivity {
private Song currsong;
public MainActivity mainActivity;
private MediaPlayer mPlayer;
public TextView songTitle , songArtist;
public ImageView playPause, next, prev;
public SeekBar seekbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_screen);
currsong = (Song)getIntent().getParcelableExtra("currSong");
final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.playScreen);
Drawable drawable=new BitmapDrawable(getResources(), currsong.getAlbumart(getApplicationContext()));
relativeLayout.setBackground(drawable);
songTitle=(TextView)findViewById(R.id.stitle);
songArtist=(TextView)findViewById(R.id.artist);
playPause= (ImageView)findViewById(R.id.playPause);
next= (ImageView)findViewById(R.id.next);
prev= (ImageView)findViewById(R.id.prev);
seekbar=(SeekBar)findViewById(R.id.progress);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mPlayer.isPlaying()){
mPlayer.stop();
try {
mPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
mainActivity.songPos++;
currsong=mainActivity.songList.get(mainActivity.songPos);
Drawable drawable=new BitmapDrawable(getResources(), currsong.getAlbumart(getApplicationContext()));
relativeLayout.setBackground(drawable);
seekbar.setMax(mPlayer.getDuration());
playSong();
}
});
prev.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mPlayer.isPlaying()){
mPlayer.stop();
try {
mPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
mainActivity.songPos--;
currsong=mainActivity.songList.get(mainActivity.songPos);
Drawable drawable=new BitmapDrawable(getResources(), currsong.getAlbumart(getApplicationContext()));
relativeLayout.setBackground(drawable);
seekbar.setMax(mPlayer.getDuration());
playSong();
}
});
playPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mPlayer.isPlaying() ){
mPlayer.pause();
playPause.setImageResource(R.drawable.play);
}
else {
mPlayer.start();
playPause.setImageResource(R.drawable.pause);
}
}
});
playSong();
}
public void playSong(){
mPlayer= new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
songTitle.setText(currsong.getTitle());
songArtist.setText(currsong.getArtist());
//set uri
Uri trackUri = ContentUris.withAppendedId(android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,currsong.getID());
//set the data source
try{
mPlayer.setDataSource(getApplicationContext(), trackUri);
}
catch(Exception e){
Log.e("MUSIC SERVICE", "Error setting data source", e);
}
try {
mPlayer.prepare();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
}
//seekbar.setMax(mPlayer.getDuration());
mPlayer.start();
playPause.setImageResource(R.drawable.pause);
}
public void initMusicPlayer(){
//set player properties
mPlayer.setWakeMode(getApplicationContext(),
PowerManager.PARTIAL_WAKE_LOCK);
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setOnPreparedListener((MediaPlayer.OnPreparedListener) this);
mPlayer.setOnCompletionListener((MediaPlayer.OnCompletionListener) this);
mPlayer.setOnErrorListener((MediaPlayer.OnErrorListener) this);
}
#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_play_screen, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

After Searching a lot i have figured out a way to do this and since no one ha answered this question so to help others with same problem I am providing my solved java file code below-
package com.musicplayer;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.PowerManager;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.StaticLayout;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.view.View.OnClickListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Random;
public class PlayScreen extends ActionBarActivity implements Runnable,
OnClickListener, OnSeekBarChangeListener, MediaPlayer.OnCompletionListener {
protected static Song currsong ;
protected TextView songTitle , songArtist;
protected ImageView playPause, next, prev, loop ,shuffle;
protected SeekBar seekbar;
protected RelativeLayout relativeLayout;
protected Uri trackpath;
protected static boolean looping=false, shuffling=false;
protected ArrayList<Song> shuffleSong;
protected MediaPlayerSingleton mPlayer = MediaPlayerSingleton.getInstance();
protected ArrayList<Song> songList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_screen);
currsong = (Song)getIntent().getParcelableExtra("currSong");
songList =new ArrayList<Song>();
Bundle b = this.getIntent().getExtras();
songList = b.getParcelableArrayList("categories");
relativeLayout = (RelativeLayout) findViewById(R.id.playScreen);
Drawable drawable=new BitmapDrawable(getResources(), currsong.getAlbumart(getApplicationContext()));
relativeLayout.setBackground(drawable);
songTitle=(TextView)findViewById(R.id.stitle);
songArtist=(TextView)findViewById(R.id.artist);
playPause= (ImageView)findViewById(R.id.playPause);
next= (ImageView)findViewById(R.id.next);
prev= (ImageView)findViewById(R.id.prev);
loop = (ImageView)findViewById(R.id.loop);
shuffle = (ImageView)findViewById(R.id.shuffle);
seekbar=(SeekBar)findViewById(R.id.progress);
next.setOnClickListener(this);
prev.setOnClickListener(this);
playPause.setOnClickListener(this);
loop.setOnClickListener(this);
shuffle.setOnClickListener(this);
seekbar.setOnSeekBarChangeListener(this);
seekbar.setEnabled(true);
loop.setImageResource(R.drawable.loop);
shuffle.setImageResource(R.drawable.shuffle);
shuffleSong = new ArrayList<Song>();
playSong();
}
public void getSongList() {
shuffleSong.addAll(songList);
Collections.shuffle(shuffleSong);
}
public void playSong(){
mPlayer.reset();
int minute = 60*1000;
Toast.makeText(getApplicationContext(), "Can't play this song: "+ currsong.getPath(), Toast.LENGTH_LONG).show();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
TextView totaltime =(TextView)findViewById(R.id.timeleft);
//set uri
Uri trackUri = ContentUris.withAppendedId(android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,currsong.getID());
trackpath=trackUri;
//set the data source
try{
mPlayer.setDataSource(getApplicationContext(), trackUri);
}
catch(Exception e){
Log.e("MUSIC SERVICE", "Error setting data source", e);
}
try {
mPlayer.prepare();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "Can't play this song: "+ currsong.getTitle(), Toast.LENGTH_LONG).show();
next.performClick();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Can't play this song: "+ currsong.getTitle(), Toast.LENGTH_LONG).show();
next.performClick();
}
songTitle.setText(currsong.getTitle());
songArtist.setText(currsong.getArtist());
seekbar.setMax(mPlayer.getDuration());
seekbar.setEnabled(true);
new Thread(this).start();
if(mPlayer.getDuration()<minute) {
totaltime.setText(String.format("0:%1$tS", new Date(mPlayer.getDuration()-60*1000*30)));
}else{
totaltime.setText(String.format("%1$tM:%1$tS", new Date(mPlayer.getDuration()-60*1000*30)));
}
mPlayer.start();
playPause.setImageResource(R.drawable.pause);
mPlayer.setOnCompletionListener(this);
}
#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_play_screen, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onClick(View v) {
if (v.equals(playPause)){
if(mPlayer == null){
try{
mPlayer.setDataSource(getApplicationContext(), trackpath);
}
catch(Exception e){
Log.e("MUSIC SERVICE", "Error setting data source", e);
}
try {
mPlayer.prepare();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
}
seekbar.setEnabled(true);
}
if (mPlayer.isPlaying()) {
mPlayer.pause();
playPause.setImageResource(R.drawable.play);
}
else {
mPlayer.start();
playPause.setImageResource(R.drawable.pause);
seekbar.setMax(mPlayer.getDuration());
new Thread(this).start();
}
}
if(v.equals(next) && mPlayer != null){
if(shuffling==false) {
if (mPlayer.isPlaying()) {
mPlayer.stop();
try {
mPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
if (looping == true) {
if (AllSongs.songPos == songList.size() - 1) {
AllSongs.songPos = 0;
currsong = songList.get(AllSongs.songPos);
} else {
AllSongs.songPos++;
currsong = songList.get(AllSongs.songPos);
}
} else {
if (AllSongs.songPos == songList.size() - 1) {
Toast.makeText(getApplicationContext(), "List Ended", Toast.LENGTH_SHORT).show();
//currsong=mainActivity.songList.get(mainActivity.songPos);
} else {
AllSongs.songPos++;
currsong = songList.get(AllSongs.songPos);
}
}
}else{
if (mPlayer.isPlaying()) {
mPlayer.stop();
try {
mPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
/*Random rand = new Random();
int randomNum = mainActivity.songPos;
while(randomNum==mainActivity.songPos) {
randomNum = rand.nextInt(shuffleSong.size());
}
currsong = shuffleSong.get(randomNum);
shuffleSong.remove(randomNum);*/
if (looping == true) {
if (AllSongs.songPos == shuffleSong.size() - 1) {
AllSongs.songPos = 0;
currsong = shuffleSong.get(AllSongs.songPos);
} else {
AllSongs.songPos++;
currsong = shuffleSong.get(AllSongs.songPos);
}
} else {
if (AllSongs.songPos == shuffleSong.size() - 1) {
Toast.makeText(getApplicationContext(), "List Ended", Toast.LENGTH_SHORT).show();
//currsong=mainActivity.songList.get(mainActivity.songPos);
} else {
AllSongs.songPos++;
currsong = shuffleSong.get(AllSongs.songPos);
}
}
}
Drawable drawable=new BitmapDrawable(getResources(), currsong.getAlbumart(getApplicationContext()));
relativeLayout.setBackground(drawable);
seekbar.setMax(mPlayer.getDuration());
seekbar.setProgress(0);
playSong();
}
if(v.equals(prev) && mPlayer != null){
if(shuffling==false) {
if(mPlayer.isPlaying()){
mPlayer.stop();
try {
mPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
if (looping == true) {
if (AllSongs.songPos == 0) {
AllSongs.songPos = songList.size() - 1;
currsong = songList.get(AllSongs.songPos);
} else {
AllSongs.songPos--;
currsong = songList.get(AllSongs.songPos);
}
} else {
if (AllSongs.songPos == 0) {
Toast.makeText(getApplicationContext(), "List Ended", Toast.LENGTH_SHORT).show();
} else {
AllSongs.songPos--;
currsong = songList.get(AllSongs.songPos);
}
}
}else{
if (mPlayer.isPlaying()) {
mPlayer.stop();
try {
mPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
if (looping == true) {
if (AllSongs.songPos == 0) {
AllSongs.songPos = shuffleSong.size() - 1;
currsong = shuffleSong.get(AllSongs.songPos);
} else {
AllSongs.songPos--;
currsong = shuffleSong.get(AllSongs.songPos);
}
} else {
if (AllSongs.songPos == 0) {
Toast.makeText(getApplicationContext(), "List Ended", Toast.LENGTH_SHORT).show();
} else {
AllSongs.songPos--;
currsong = shuffleSong.get(AllSongs.songPos);
}
}
/*Random rand = new Random();
int randomNum = mainActivity.songPos;
while(randomNum==mainActivity.songPos) {
randomNum = rand.nextInt(shuffleSong.size());
}
currsong = shuffleSong.get(randomNum);
shuffleSong.remove(randomNum);*/
}
Drawable drawable=new BitmapDrawable(getResources(), currsong.getAlbumart(getApplicationContext()));
relativeLayout.setBackground(drawable);
seekbar.setMax(mPlayer.getDuration());
seekbar.setProgress(0);
playSong();
}
if(v.equals(loop)){
if(looping==false) {
loop.setImageResource(R.drawable.loopactive);
looping = true;
}
else{
looping=false;
loop.setImageResource(R.drawable.loop);
}
}
if(v.equals(shuffle)){
if(shuffling==false) {
getSongList();
shuffle.setImageResource(R.drawable.shuffleactive);
shuffling = true;
Collections.shuffle(shuffleSong);
//setNextSongDetails();
//shuffleList();
}else{
shuffleSong.clear();
shuffling=false;
shuffle.setImageResource(R.drawable.shuffle);
//setNextSongDetails();
}
}
}
/*private void shuffleList() {
for(int i=0;i<mainActivity.songList.size();i++){
Random rand = new Random();
int randomNum = rand.nextInt((mainActivity.songList.size() - i)) + i;
int j=0,k=0;
int flag=0;
k=mainActivity.posList.size();
for(j=0;j<k;j++){
if(MainActivity.posList.get(j) == randomNum){
flag=1;
}
//j=j+1;
}
if(flag==1){
i=i-1;
}else{
MainActivity.posList.add(new Integer(randomNum));
tempsong=MainActivity.songList.get(randomNum);
shuffleSong.add(new Song(tempsong.getID(), tempsong.getTitle(), tempsong.getArtist(), tempsong.getAlbumID()));
}
}
}*/
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
try {
if (mPlayer.isPlaying() || mPlayer != null) {
if (fromUser)
mPlayer.seekTo(progress);
} else if (mPlayer == null) {
Toast.makeText(getApplicationContext(), "Media is not running",
Toast.LENGTH_SHORT).show();
seekBar.setProgress(0);
}
} catch (Exception e) {
//Log.e("seek bar", "" + e);
seekBar.setEnabled(false);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
#Override
public void run() {
int currentPosition = mPlayer.getCurrentPosition();
int total = mPlayer.getDuration();
while (mPlayer != null && currentPosition < total) {
try {
Thread.sleep(1000);
currentPosition = mPlayer.getCurrentPosition();
} catch (InterruptedException e) {
return;
} catch (Exception e) {
return;
}
seekbar.setProgress(currentPosition);
}
}
#Override
public void onCompletion(MediaPlayer mp) {
if(mPlayer.getCurrentPosition()>0){
mp.reset();
next.performClick();
}
}
}

Related

android webview java.lang.NullPointerException: Attempt to invoke virtual method 'android.webkit.WebSettings on a null object reference

I'm trying to implement swipe in a customized WebView, but when the app tries to call getSettings(), a null pointer exception is thrown. Here are the particulars:
First, the extended WebView class:
package com.chiaramail.opencomments;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.webkit.WebView;
import android.widget.Toast;
public class OpenCommentsWebView extends WebView {
Context context;
GestureDetector gd;
public OpenCommentsWebView(Context context, AttributeSet attrs) {
super(context);
this.context = context;
gd = new GestureDetector(context, sogl);
}
GestureDetector.SimpleOnGestureListener sogl = new GestureDetector.SimpleOnGestureListener() {
public boolean onDown(MotionEvent event) {
return true;
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (e1 == null || e2 == null) return false;
if (e1.getPointerCount() > 1 || e2.getPointerCount() > 1) return false;
else {
try { // Swipe down, so set
if (e2.getY() - e1.getY() > 100 && Math.abs(velocityY) > 800) {
MainActivity.recentCommentsShown = false;
return true;
}
} catch (Exception e) { // nothing
Log.e("OpenCommentsWebView", "Swipe error: " + e.getLocalizedMessage());
// Toast.makeText(this, getString(R.string.update_flow_failed) + BLANK + e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
}
return false;
}
}
};
void show_toast(final String text) {
Toast t = Toast.makeText(context, text, Toast.LENGTH_SHORT);
t.show();
}
}
The relevant part of the main layout file:
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.chiaramail.opencomments.OpenCommentsWebView
android:id="#+id/browser_page"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
Instantiating the extended WebView object:
private OpenCommentsWebView browser_page;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = this;
try {
setContentView(R.layout.main);
} catch (RuntimeException ex) {
ex.printStackTrace();
Toast.makeText(activity, getString(R.string.install_from_google_play), Toast.LENGTH_LONG).show();
finish();
return;
}
current_page = "https://www.google.com/";
browser_page = findViewById(R.id.browser_page);
#Override
public void onStart() {
super.onStart();
WebAction();
}
.
.
.
public void WebAction() {
// Fails here.
browser_page.getSettings().setJavaScriptEnabled(true);
}

android.hardware.camera2 on react-native

I'm trying to make a react-native project where i can enter the camera on the native side by clicking on a button on the react side.
it needs to click a single pic and send the path of file back to react Everything works fine. the file is being saved (1600X1200 resolution) but its not returning to react side and an error is coming i.e.:-
2019-05-30 15:32:49.497 28080-28132/com.gjcamera E/BufferItemConsumer: [ImageReader-1600x1200f100m1-28080-0] Failed to release buffer: Unknown error -1 (1).
I don't know where i am making a mistake.
here's my code:-
App.js:-
import React, { Component } from 'react';
import { Platform, StyleSheet, Text, View, TouchableOpacity, NativeModules } from 'react-native';
const GJC = NativeModules.GJCamera;
type Props = {};
export default class App extends Component<Props> {
constructor() {
super();
this.state = {
imagePathh: "empty",
}
this.OpenMyCamera = this.OpenMyCamera.bind();
}
OpenMyCamera = async () => {
const result = await GJC.openCamera();
this.setState({ imagePathh: result });
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>Welcome to React Native!</Text>
<Text style={styles.instructions}>To enter camera, click on the "Camera" below</Text>
<Text style={styles.instructions}>The path of image will be shown below...</Text>
<TouchableOpacity style={{ backgroundColor: 'green' }} onPress={() => this.OpenMyCamera()}>
<Text style={{ color: '#fff' }}>Camera</Text>
</TouchableOpacity>
<Text style={{ paddingTop: 300 }}>The Path of image is </Text>
<Text style={{ color: "skyblue" }}>{this.state.imagePathh}</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
GJCameraModule.java:-
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.widget.Toast;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import java.util.HashMap;
import java.util.Map;
import static android.support.v4.content.ContextCompat.startActivity;
public class GJCameraModule extends ReactContextBaseJavaModule {
private Promise promise;
public GJCameraModule(ReactApplicationContext reactContext) {
super(reactContext);
}
#ReactMethod
public String show() {
String imagePath = "";
String imageName = "";
imageName = "";//GJCamera.sendPath();
imagePath = Environment.getExternalStorageDirectory() + "/DCIM/Camera/" + imageName + ".jpg";
return imagePath.toString();
}
#ReactMethod
public void openCamera(final Promise promise) {
Toast.makeText(getReactApplicationContext(), "LOLZ", Toast.LENGTH_SHORT).show();
try {
GJCamera gjc = GJCamera.newInstance();
gjc.setPromise(promise);
Intent intent = new Intent(getCurrentActivity(), GJCamera.class);
getCurrentActivity().startActivity(intent);
Toast.makeText(getReactApplicationContext(), "LOLZ--Return", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
promise.reject(e.getMessage(), e.getMessage());
}
}
#Override
public String getName() {
return "GJCamera";
}
#Override
public Map<String, Object> getConstants() {
final Map<String, Object> constants = new HashMap<>();
String android_id = Settings.System.getString(getReactApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);
constants.put("uniqueId", android_id);
return constants;
}
}
GJCamera.java:-
package com.gjcamera;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.ImageFormat;
import android.graphics.SurfaceTexture;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CameraMetadata;
import android.hardware.camera2.CaptureRequest;
import android.hardware.camera2.TotalCaptureResult;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.media.Image;
import android.media.ImageReader;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.os.HandlerThread;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Size;
import android.util.SparseIntArray;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.WritableMap;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.UUID;
public class GJCamera extends AppCompatActivity {
private ImageButton btnCapture;
private TextureView textureView;
public static String imgName;
private static Promise promise;
//Check state orientation of output image
private static final SparseIntArray ORIENTATIONS = new SparseIntArray();
static {
ORIENTATIONS.append(Surface.ROTATION_0, 90);
ORIENTATIONS.append(Surface.ROTATION_90, 0);
ORIENTATIONS.append(Surface.ROTATION_180, 270);
ORIENTATIONS.append(Surface.ROTATION_270, 180);
}
private String cameraId;
private CameraDevice cameraDevice;
private CameraCaptureSession cameraCaptureSessions;
private CaptureRequest.Builder captureRequestBuilder;
private Size imageDimension;
private ImageReader imageReader;
//Save to FILE
private File file;
private static final int REQUEST_CAMERA_PERMISSION = 200;
private boolean mFlashSupported;
private Handler mBackgroundHandler;
private HandlerThread mBackgroundThread;
SurfaceTexture texture;
public static GJCamera newInstance() {
GJCamera a = new GJCamera();
return a;
}
CameraDevice.StateCallback stateCallback = new CameraDevice.StateCallback() {
#Override
public void onOpened(#NonNull CameraDevice camera) {
cameraDevice = camera;
createCameraPreview();
}
#Override
public void onDisconnected(#NonNull CameraDevice camera) {
// if (promise!=null) { promise.reject("Error");}
// promise = null;
cameraDevice = camera;
returnHome();
}
#Override
public void onError(#NonNull CameraDevice camera, int i) {
// if (promise!=null) { promise.reject("Error");}
// promise = null;
cameraDevice = camera;
returnHome();
}
};
public void setPromise(Promise promise) {
this.promise = promise;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gjcamera);
textureView = findViewById(R.id.textureView);
//From Java 1.4 , you can use keyword 'assert' to check expression true or false
assert textureView != null;
textureView.setSurfaceTextureListener(textureListener);
btnCapture = findViewById(R.id.clickButton);
btnCapture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
takePicture();
}
});
}
private void takePicture() {
if (cameraDevice == null)
return;
CameraManager manager = null;
manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraDevice.getId());
Size[] jpegSizes = null;
if (characteristics != null)
jpegSizes = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)
.getOutputSizes(ImageFormat.JPEG);
//Capture image with custom size
int width = 1600;
int height = 1200;
imageReader = ImageReader.newInstance(width, height, ImageFormat.JPEG, 1);
List<Surface> outputSurface = new ArrayList<>(2);
outputSurface.add(imageReader.getSurface());
outputSurface.add(new Surface(textureView.getSurfaceTexture()));
final CaptureRequest.Builder captureBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
captureBuilder.addTarget(imageReader.getSurface());
captureBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
//Check orientation base on device
int rotation = getWindowManager().getDefaultDisplay().getRotation();
captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, ORIENTATIONS.get(rotation));
Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
file = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera/IMG_" + calendar.get(Calendar.YEAR) + calendar.get(Calendar.MONTH) + calendar.get(Calendar.DATE) + "_" + calendar.get(Calendar.HOUR_OF_DAY) + calendar.get(Calendar.MINUTE) + calendar.get(Calendar.SECOND) + ".jpg");
ImageReader.OnImageAvailableListener readerListener = null;
readerListener = new ImageReader.OnImageAvailableListener() {
#Override
public void onImageAvailable(ImageReader imageReadr) {
Image image = null;
try {
image = imageReader.acquireLatestImage();
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.capacity()];
buffer.get(bytes);
save(bytes);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
{
if (image != null)
image.close();
}
}
}
private void save(byte[] bytes) throws IOException {
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file);
outputStream.write(bytes);
} finally {
if (outputStream != null)
outputStream.close();
}
}
};
imageReader.setOnImageAvailableListener(readerListener, mBackgroundHandler);
final CameraCaptureSession.CaptureCallback captureListener = new CameraCaptureSession.CaptureCallback() {
#Override
public void onCaptureCompleted(#NonNull CameraCaptureSession session, #NonNull CaptureRequest request, #NonNull TotalCaptureResult result) {
super.onCaptureCompleted(session, request, result);
Toast.makeText(GJCamera.this, "Saved " + file, Toast.LENGTH_SHORT).show();
returnHome();
}
};
cameraDevice.createCaptureSession(outputSurface, new CameraCaptureSession.StateCallback() {
#Override
public void onConfigured(#NonNull CameraCaptureSession cameraCaptureSession) {
try {
cameraCaptureSession.capture(captureBuilder.build(), captureListener, mBackgroundHandler);
// cameraDevice.close();
//convertFileToWritableMap(file));
// promise = null;
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
#Override
public void onConfigureFailed(#NonNull CameraCaptureSession cameraCaptureSession) {
}
}, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
private void returnHome() {
texture.release();
// if(imageReader != null) {
// imageReader.close();
// imageReader = null;
// }
if (cameraDevice != null) {
cameraDevice.close();
cameraDevice = null;
}
if (cameraCaptureSessions != null) {
cameraCaptureSessions.close();
cameraCaptureSessions = null;
}
if (promise != null) {
promise.resolve(convertFileToWritableMap(file));
promise = null;
}
}
private void createCameraPreview() {
try {
texture = textureView.getSurfaceTexture();
assert texture != null;
texture.setDefaultBufferSize(1600, 1200);
Surface surface = new Surface(texture);
captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
captureRequestBuilder.addTarget(surface);
cameraDevice.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback() {
#Override
public void onConfigured(#NonNull CameraCaptureSession cameraCaptureSession) {
if (cameraDevice == null)
return;
cameraCaptureSessions = cameraCaptureSession;
updatePreview();
}
#Override
public void onConfigureFailed(#NonNull CameraCaptureSession cameraCaptureSession) {
Toast.makeText(GJCamera.this, "Changed", Toast.LENGTH_SHORT).show();
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
private void updatePreview() {
if (cameraDevice == null)
Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
captureRequestBuilder.set(CaptureRequest.CONTROL_MODE, CaptureRequest.CONTROL_MODE_AUTO);
try {
cameraCaptureSessions.setRepeatingRequest(captureRequestBuilder.build(), null, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
private void openCamera() {
CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
cameraId = manager.getCameraIdList()[0];
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
assert map != null;
imageDimension = map.getOutputSizes(SurfaceTexture.class)[0];
//Check realtime permission if run higher API 23
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE
}, REQUEST_CAMERA_PERMISSION);
return;
}
manager.openCamera(cameraId, stateCallback, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
TextureView.SurfaceTextureListener textureListener = new TextureView.SurfaceTextureListener() {
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) {
openCamera();
}
#Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1) {
}
#Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
return false;
}
#Override
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
}
};
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if (requestCode == REQUEST_CAMERA_PERMISSION) {
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "You can't use camera without permission", Toast.LENGTH_SHORT).show();
finish();
}
}
}
#Override
protected void onResume() {
super.onResume();
startBackgroundThread();
if (textureView.isAvailable())
openCamera();
else
textureView.setSurfaceTextureListener(textureListener);
}
#Override
protected void onPause() {
stopBackgroundThread();
super.onPause();
}
private void stopBackgroundThread() {
mBackgroundThread.quitSafely();
try {
mBackgroundThread.join();
mBackgroundThread = null;
mBackgroundHandler = null;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void startBackgroundThread() {
mBackgroundThread = new HandlerThread("Camera Background");
mBackgroundThread.start();
mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
}
public static WritableMap convertFileToWritableMap(File fileLocation) {
WritableMap newFile = Arguments.createMap();
if (fileLocation == null) return newFile;
newFile.putString("imgPath", fileLocation.getPath());
return newFile;
}
}
activity_gjcamera.xml:-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.gjcamera.MainActivity">
<TextureView
android:id="#+id/textureView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/clickButton"
/>
<ImageButton
android:id="#+id/clickButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#drawable/camera_click" />
</RelativeLayout>
BTW, Thanks for the help.
finally, found it:-
in GJCamera.java within returnHome() Method:-
if (promise != null) {
promise.resolve(convertFileToWritableMap(file));
promise = null;
}
change it to:-
if (promise != null) {
promise.resolve(convertFileToWritableMap(file));
promise = null;
finish(); // in order to finish the activity.
}
The entire project is present Here.

How can I decide if the game I coded using JavaFX uses adequate system resources?

I have coded a simple card game (using IntelliJ 3.3) that updates gui each second and contains many nodes in game which are used to display game events and animation. People who have tried the game said that their system slows down and memory and cpu usage goes up (I was shocked when I saw from one of my friend's screen that it used around 1.4 GB ram) is this supposed to happen?
I have checked some questions related to javaFX memory usage problems but could not relate to my matter. In one article, I read that using many nodes in a JavaFX game might result in high memory usage.
in a short runtime:
minimum memory usage is around 100-250 MB
maximum memory usage is around 800-1200 MB
average memory usage is around 500-700 MB
IMPORTANT:
THERE ARE 100 BUTTONS IN A 10X10 GRIDPANE WHICH DO NOT HAVE FX IDS
in gameButtonClicked matches are found buttons.indexOf(event.getSource())
UML DIAGRAM
Main.java
IMPORTS:
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;
public class Main extends Application {
static boolean isMuted;
static Stage window;
static MediaPlayer mediaPlayerBGM;
static MediaPlayer mediaPlayerSFX;
private static HashMap<String, Media> sounds;
private static ArrayList<Long> usage;
private final String[] SOUND_LIST = {"bgm_credits.mp3", "bgm_game.mp3", "bgm_game_1.mp3", "bgm_game_2.mp3", "bgm_game_3.mp3",
"bgm_how_to.mp3", "bgm_menu.mp3", "bgm_victory.mp3", "sfx_button_clicked.wav",
"sfx_card_unfold.wav", "sfx_toggle.wav"
};
public static void main(String[] args) {
launch(args);
AtomicLong sum = new AtomicLong();
usage.forEach(sum::addAndGet);
long average = sum.get() / usage.size();
System.out.printf("minimum usage: %d, maximum usage: %d, average usage: %d",
Collections.min(usage), Collections.max(usage), average);
}
static void playBGM(String key) {
mediaPlayerBGM.stop();
mediaPlayerBGM.setStartTime(Duration.ZERO);
if (key.equals("bgm_game")) {
String[] suffix = {"", "_1", "_2", "_3"};
Random random = new Random();
mediaPlayerBGM = new MediaPlayer(sounds.get(key + suffix[random.nextInt(4)]));
} else {
mediaPlayerBGM = new MediaPlayer(sounds.get(key));
}
mediaPlayerBGM.setCycleCount(MediaPlayer.INDEFINITE);
if (isMuted) {
mediaPlayerBGM.setVolume(0.0);
}
mediaPlayerBGM.play();
}
static void playSFX(String key) {
if (mediaPlayerSFX != null) {
mediaPlayerSFX.stop();
}
mediaPlayerSFX = new MediaPlayer(sounds.get(key));
if (isMuted) {
mediaPlayerSFX.setVolume(0.0);
}
mediaPlayerSFX.play();
}
#Override
public void start(Stage primaryStage) throws Exception {
sounds = new HashMap<>();
usage = new ArrayList<>();
isMuted = false;
for (String soundName :
SOUND_LIST) {
URL resource = getClass().getResource("/" + soundName);
System.out.println(soundName);
System.out.println(resource.toString());
sounds.put(soundName.substring(0, soundName.lastIndexOf('.')), new Media(resource.toString()));
}
mediaPlayerBGM = new MediaPlayer(sounds.get("bgm_menu"));
mediaPlayerBGM.setCycleCount(MediaPlayer.INDEFINITE);
mediaPlayerBGM.play();
window = primaryStage;
Parent root = FXMLLoader.load(getClass().getResource("menu.fxml"));
// long running operation runs on different thread
Thread thread = new Thread(() -> {
Runnable updater = () -> {
if (!Game.isGameIsOver() && Game.getScore() != 0 && window.getTitle().equals("The Main Pick") &&
Game.firstClickHappened()) {
Game.scoreCalculator();
}
};
while (true) {
try {
usage.add(Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory());
System.out.printf("Used memory: %d\n", Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory());
Thread.sleep(1000);
} catch (InterruptedException ex) {
System.out.println("Interrupted");
}
// UI update is run on the Application thread
Platform.runLater(updater);
}
});
// don't let thread prevent JVM shutdown
thread.setDaemon(true);
thread.start();
window.setTitle("Main Menu");
window.setScene(new Scene(root, 600, 600));
window.setResizable(false);
window.show();
}
}
Controller.java
IMPORTS:
package com.sample;
import javafx.animation.FadeTransition;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
importjavafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.layout.GridPane;
import javafx.util.Duration;
import java.io.IOException;
public class Controller
{
#FXML
public RadioButton musicOnOff;
#FXML
public Button newGame;
#FXML
public Button howTo;
#FXML
public Button credits;
#FXML
public Button exit;
#FXML
public Button menu;
#FXML
public GridPane pane;
#FXML
public Label score;
#FXML
public Label time;
#FXML
public Label tries;
private boolean animating;
public void initialize() {
if (score != null && time != null && tries != null) {
score.textProperty().bind(Game.scoreProperty);
time.textProperty().bind(Game.timeProperty);
tries.textProperty().bind(Game.triesProperty);
animating=false;
}
if (musicOnOff!=null){
if(Main.isMuted){
musicOnOff.setSelected(true);
}
}
}
public void newGameButtonClicked() {
try {
Main.playSFX("sfx_button_clicked");
new Game();
Main.window.hide();
Main.window.setScene(getScene("game"));
Main.window.setTitle("The Main Pick");
Main.window.setMaximized(true);
Main.playBGM("bgm_game");
Main.window.show();
} catch (IOException e) {
System.out.println("could not change the scene to: game");
}
}
public void menuButtonClicked() {
try {
Main.playSFX("sfx_button_clicked");
Main.playBGM("bgm_menu");
if (Main.window.getTitle().equals("The Main Pick")) {
Main.window.hide();
Game.setGameOver();
Main.window.setScene(getScene("menu"));
Main.window.setMaximized(false);
Main.window.show();
} else {
Main.window.setScene(getScene("menu"));
}
Main.window.setTitle("Main Menu");
} catch (IOException e) {
System.out.println("could not change the scene to: game");
}
}
public void gameButtonClicked(ActionEvent event) {
ObservableList<Node> buttons = pane.getChildren();
int index = buttons.indexOf(event.getSource());
int column = index % 10;
int row = (index - index % 10) / 10;
if (!((Button) event.getSource()).getStyleClass().toString().equals("button button-treasure") &&
!((Button) event.getSource()).getStyleClass().toString().equals("button button-uncovered"))
{
FadeTransition transition = new FadeTransition();
transition.setNode((Button) event.getSource());
transition.setDuration(new Duration(500));
transition.setFromValue(1.0);
transition.setToValue(0.0);
transition.setCycleCount(1);
transition.setOnFinished(actionEvent -> {
if (((Button) event.getSource()).getStyleClass().toString().equals("button button-treasure")) {
loadVictoryScene();
}
if (!((Button) event.getSource()).getStyleClass().toString().equals("button button-treasure") &&
!((Button) event.getSource()).getStyleClass().toString().equals("button button-uncovered"))
{
transition.setFromValue(0.0);
transition.setToValue(1.0);
System.out.println(((Button) event.getSource()).getStyleClass().toString());
((Button) event.getSource()).getStyleClass().remove("button-covered");
((Button) event.getSource()).getStyleClass().add(Game.click(row, column));
transition.play();
transition.setOnFinished(ActionEvent->animating=false);
}
});
System.out.println(animating);
if(!animating){
animating=true;
transition.play();
Main.playSFX("sfx_card_unfold");}
}
System.out.printf("button index:%d,row:%d,column:%d\n", index, row, column);
}
public void howToPlayButtonClicked() {
try {
Main.playSFX("sfx_button_clicked");
Main.playBGM("bgm_how_to");
Main.window.setScene(getScene("howTo"));
Main.window.setTitle("How to Play");
} catch (IOException e) {
System.out.println("could not change the scene to: how to play");
}
}
public void creditsButtonClicked() {
try {
Main.playSFX("sfx_button_clicked");
Main.playBGM("bgm_credits");
Main.window.setScene(getScene("credits"));
Main.window.setTitle("Credits");
} catch (IOException e) {
System.out.println("could not change the scene to: credits");
}
}
public void exitButtonClicked() {
Main.playSFX("sfx_button_clicked");
Main.window.close();
}
public void musicOnOffRadioButtonChecked() {
Main.playSFX("sfx_toggle");
if (musicOnOff.isSelected()) {
Main.isMuted=true;
Main.mediaPlayerBGM.setVolume(0.0);
Main.mediaPlayerSFX.setVolume(0.0);
System.out.println("now selected");
} else {
Main.isMuted=false;
Main.mediaPlayerBGM.setVolume(1.0);
Main.mediaPlayerSFX.setVolume(1.0);
System.out.println("unselected");
}
}
private void loadVictoryScene() {
try {
Main.window.hide();
Main.playBGM("bgm_victory");
Main.window.setScene(getScene("victory"));
Main.window.setTitle("Victory");
Main.window.setMaximized(false);
Main.window.show();
} catch (IOException e) {
System.out.println("could not change the scene to: victory");
}
}
private Scene getScene(String name) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource(name + ".fxml"));
if (name.equals("game")) {
return new Scene(root, 800, 800);
}
return new Scene(root, 600, 600);
}
}
Game.java
IMPORTS:
package com.sample;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import java.util.Random;
public class Game {
static StringProperty scoreProperty;
static StringProperty timeProperty;
static StringProperty triesProperty;
private static int time;
private static int score;
private static int tries;
private static int[][] tiles;
private static boolean gameIsOver;
private static boolean firstClick;
public Game() {
System.out.println("Game created...");
tries = 0;
score = 100000;
time = 0;
gameIsOver = false;
firstClick = false;
scoreProperty = new SimpleStringProperty("" + score);
timeProperty = new SimpleStringProperty("0");
triesProperty = new SimpleStringProperty("0");
tiles = new int[10][10];
Random random = new Random();
int treasureColumn = random.nextInt(10);
int treasureRow = random.nextInt(10);
tiles[treasureRow][treasureColumn] = 1;
}
static boolean firstClickHappened() {
return firstClick;
}
static void setGameOver() {
gameIsOver = true;
}
static boolean isGameIsOver() {
return gameIsOver;
}
static int getScore() {
return score;
}
static void scoreCalculator() {
time++;
if (time < 10) {
score = score - 100;
} else if (time < 20) {
score = score - 200;
} else if (time < 30) {
score = score - 300;
} else if (time < 50) {
score = score - 500;
} else {
score = score - 1000;
}
if (score < 0) {
score = 0;
}
scoreProperty.setValue("" + score);
timeProperty.setValue("" + time);
triesProperty.setValue("" + tries);
System.out.printf("Score:%s,Time:%s,Tries%s\n", scoreProperty.getValue(), timeProperty.getValue(), triesProperty.getValue());
}
static String click(int row, int column) {
if (!firstClickHappened()) firstClick = true;
System.out.println(row + "," + column);
int clickValue = tiles[row][column];
System.out.println(clickValue);
if (clickValue == 0) {
tries++;
score -= 1000;
} else {
setGameOver();
}
return (clickValue == 1) ? "button-treasure" : "button-uncovered";
}
}

Problems with some Automation Testing using Before / After / Test

Problems with some Automation Testing using Before / After / Test
I'm getting stuck at the browser window, no test is executed...
Not sure why this doesn't work. I've exported some tests from Selenium IDE, exported as Java, edited that stuff in Eclipse, yet still it doesn't work...
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class Checkimage {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
#Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#Test
public void testCheckimage() throws Exception {
driver.get("http://");
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (isElementPresent(By.linkText("sign in / up"))) break; } catch (Exception e) {}
Thread.sleep(1000);
}
driver.findElement(By.id("login-email")).clear();
driver.findElement(By.id("login-email")).sendKeys("michael.sinitsin#avid.com");
driver.findElement(By.id("login-password")).clear();
driver.findElement(By.id("login-password")).sendKeys("mike1780");
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if ("sign in".equals(driver.findElement(By.id("login-buttons-password")).getText())) break; } catch (Exception e) {}
Thread.sleep(1000);
}
driver.findElement(By.id("login-buttons-password")).click();
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if ("Tester".equals(driver.findElement(By.linkText("Tester")).getText())) break; } catch (Exception e) {}
Thread.sleep(1000);
}
driver.findElement(By.linkText("Tester")).click();
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (isElementPresent(By.id("inputScoreUrl"))) break; } catch (Exception e) {}
Thread.sleep(1000);
}
driver.findElement(By.id("inputScoreUrl")).sendKeys("http://www.sheetmusicdirect.com/scorches/smd_000001.sco");
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if ("Enter a URL to a publicly accessible SCO file. No need to URL encode it, just copy and paste it in.".equals(driver.findElement(By.cssSelector("p.help-block")).getText())) break; } catch (Exception e) {}
Thread.sleep(1000);
}
driver.findElement(By.id("submitScoreView")).click();
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (isElementPresent(By.cssSelector("img"))) break; } catch (Exception e) {}
Thread.sleep(1000);
}
try {
assertTrue(isElementPresent(By.cssSelector("img")));
} catch (Error e) {
verificationErrors.append(e.toString());
}
}
#After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
private String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alertText;
} finally {
acceptNextAlert = true;
Not sure why this doesn't work. I've exported some tests from Selenium IDE, exported as Java, edited that stuff in Eclipse, yet still it doesn't work...
Dear Michael Sinitsin,
As I see it you have here several issues:
You do not supply a valid URL to the web driver
replace driver.get("http://"); with a valid URL like driver.get("http://www.google.com");
You are exposing your user/password on a public site
Michael Sinitsin,
you should first mention proper URl driver.get("http://"); And your script is too complex,you could write it in a simplest way.

Panning and Zooming on MapView

I am trying to create an app that uses the panning and zooming features on the map. I have spent a lot of time trying to implement this. I first tried to create an on touch method inside the MapActivity, but I soon realized it would be a little more than that. I found some code that I tried to implement that created a subclass of a mapview and ran into some issues with it. I need some help with panning around the map. Everywhere I try to clear and display the overlay causes problems. Either the overlays get removed and displayed at the wrong place, or a loop is created causing flashing. Please let me know what I need to do to get this working. I realized that I may need to override the onScroll method if there is one instead of the onTouch method. Right now it is pretty buggy I need some help, so that I can make it bug free. Let me know if my thinking is correct. I need to get this up on the market right away. Here is my code. Thanks in advance.
//MapActivity class
//Package name omitted
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
public class MapActivityNearby extends MapActivity {
private static final String TAG = "MAPACTIVITY";
double lat;
double lng;
EnhancedMapView mv;
ArrayList<MapItem> allCats;
private static final String PREF_NAME = "cookie";
private float latMax;
private float latMin;
private float longMax;
private float longMin;
GeoPoint p;
private List<Overlay> mapOverlays;
private MyItemizedOverlay itemizedOverlay;
boolean waitTime = false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_nearby);
LinearLayout ll = (LinearLayout) findViewById(R.id.maplayout);
mv = new EnhancedMapView(this, "0ieXSx8GEy9Hm7bCZMLckus7pmPKg0w8kelRO_g");
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
mv.setLayoutParams(lp);
mv.setClickable(true);
mv.setBuiltInZoomControls(true);
mv.setOnZoomChangeListener(new EnhancedMapView.OnZoomChangeListener() {
#Override
public void onZoomChange(MapView view, int newZoom, int oldZoom) {
Log.v("test", "zoom center changed");
if (isNetworkAvailable(MapActivityNearby.this))
{
LogIn log = new LogIn();
log.execute(mv);
}
else
{
AlertDialog.Builder builder = new AlertDialog.Builder(MapActivityNearby.this);
builder.setMessage("No network connection. Please try again when your within coverage area.")
.setTitle("Network Connection")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//close dialog
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
});
mv.setOnPanChangeListener(new EnhancedMapView.OnPanChangeListener() {
public void onPanChange(MapView view, GeoPoint newCenter, GeoPoint oldCenter) {
Log.v("test", "pan center changed");
if (isNetworkAvailable(MapActivityNearby.this))
{
LogIn log = new LogIn();
log.execute(mv);
}
else
{
AlertDialog.Builder builder = new AlertDialog.Builder(MapActivityNearby.this);
builder.setMessage("No network connection. Please try again when your within coverage area.")
.setTitle("Network Connection")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//close dialog
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
});
ll.addView(mv);
SharedPreferences cookies = getSharedPreferences(PREF_NAME,
Context.MODE_PRIVATE);
lat = Double.parseDouble(cookies.getString("lat", "0"));
lng = Double.parseDouble(cookies.getString("lng", "0"));
GeoPoint p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
MapController mapControl = mv.getController();
mapControl.setCenter(p);
mapControl.setZoom(11);
}
public void onResume()
{
super.onResume();
if (isNetworkAvailable(MapActivityNearby.this))
{
LogIn log = new LogIn();
log.execute(mv);
}
else
{
AlertDialog.Builder builder = new AlertDialog.Builder(MapActivityNearby.this);
builder.setMessage("No network connection. Please try again when your within coverage area.")
.setTitle("Network Connection")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//close dialog
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
public void plotPoints(ArrayList<MapItem> i) {
mapOverlays = mv.getOverlays();
// first overlay
Drawable drawable;
drawable = getResources().getDrawable(R.drawable.marker);
itemizedOverlay = new MyItemizedOverlay(drawable, mv);
mapOverlays.add(itemizedOverlay);
for (MapItem x : i) {
GeoPoint point = new GeoPoint((int) (x.getLat() * 1E6),
(int) (x.getLng() * 1E6));
OverlayItem overlayItem = new OverlayItem(point,
x.getTitle(), x.getSubtitle());
itemizedOverlay.addOverlay(overlayItem);
}
}
private class LogIn extends AsyncTask<EnhancedMapView, Void, Boolean> {
String result = "";
InputStream is;
#Override
protected void onPreExecute() {
}
#Override
protected Boolean doInBackground(EnhancedMapView...params) {
if (!(mv.getOverlays().isEmpty()))
{
mv.getOverlays().clear();
}
int maxCount = 100;
EnhancedMapView mapView = params[0];
for (int i = 0; i < maxCount; i++)
{
p = mapView.getMapCenter();
float latCenter = (float) (p.getLatitudeE6()) / 1000000;
float longCenter = (float) (p.getLongitudeE6()) / 1000000;
float latSpan = (float) (mapView.getLatitudeSpan()) / 1000000;
float longSpan = (float) (mapView.getLongitudeSpan()) / 1000000;
latMax = latCenter + (latSpan/2);
latMin = latCenter - (latSpan/2);
longMax = longCenter + (longSpan/2);
longMin = longCenter - (longSpan/2);
if (latMin == latMax)
{
try
{
Thread.sleep(80);
}
catch(InterruptedException e)
{
}
}
else
{
p = mapView.getMapCenter();
latCenter = (float) (p.getLatitudeE6()) / 1000000;
longCenter = (float) (p.getLongitudeE6()) / 1000000;
latSpan = (float) (mapView.getLatitudeSpan()) / 1000000;
longSpan = (float) (mapView.getLongitudeSpan()) / 1000000;
latMax = latCenter + (latSpan/2);
latMin = latCenter - (latSpan/2);
longMax = longCenter + (longSpan/2);
longMin = longCenter - (longSpan/2);
break;
}
}
log(latMin);
log(latMax);
try {
final String catURL = "url goes here";
log(catURL.toString());
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(catURL);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
waitTime = true;
nameValuePairs.add(new BasicNameValuePair("PHPSESSID", getCookie()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
result = convertStreamToString();
log(result);
allCats = new ArrayList<MapItem>();
JSONObject object = new JSONObject(result);
JSONArray temp = object.getJSONArray("l");
log("Starting download");
log(temp.length());
long start = System.currentTimeMillis();
for (int k = 0; k < temp.length(); k++) {
JSONObject j = temp.getJSONObject(k);
MapItem c = new MapItem();
c.setObject_id(j.getInt("object_id"));
c.setTitle(j.getString("title"));
c.setColor(j.getString("color"));
c.setLat(j.getDouble("lat"));
c.setLng(j.getDouble("lng"));
c.setSubtitle(j.getString("subtitle"));
allCats.add(c);
log(allCats.toString());
}
long end = System.currentTimeMillis();
log("Download Took: " + (end - start) / 1000 + " seconds.");
// log(allCats.toString());
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
#Override
protected void onPostExecute(Boolean result) {
// pBar.setVisibility(View.GONE);
plotPoints(allCats);
}
private String convertStreamToString() {
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
result.trim();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
return result;
}
}
public String getCookie() {
String cookie = "";
SharedPreferences cookies = getSharedPreferences(PREF_NAME,
Context.MODE_PRIVATE);
if (cookies.contains("cookie")) {
cookie = cookies.getString("cookie", "null");
}
return cookie;
}
private void log(Object obj) {
Log.d(TAG, TAG + " :: " + obj.toString());
}
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
// CustomMapView class
// package name omitted
import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
public class EnhancedMapView extends MapView {
public interface OnZoomChangeListener {
public void onZoomChange(MapView view, int newZoom, int oldZoom);
}
public interface OnPanChangeListener {
public void onPanChange(MapView view, GeoPoint newCenter, GeoPoint oldCenter);
}
private EnhancedMapView _this;
// Set this variable to your preferred timeout
private long events_timeout = 500L;
private boolean is_touched = false;
private GeoPoint last_center_pos;
private int last_zoom;
private Timer zoom_event_delay_timer = new Timer();
private Timer pan_event_delay_timer = new Timer();
private EnhancedMapView.OnZoomChangeListener zoom_change_listener;
private EnhancedMapView.OnPanChangeListener pan_change_listener;
public EnhancedMapView(Context context, String apiKey) {
super(context, apiKey);
_this = this;
last_center_pos = this.getMapCenter();
last_zoom = this.getZoomLevel();
}
public EnhancedMapView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public EnhancedMapView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setOnZoomChangeListener(EnhancedMapView.OnZoomChangeListener l) {
zoom_change_listener = l;
}
public void setOnPanChangeListener(EnhancedMapView.OnPanChangeListener l) {
pan_change_listener = l;
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
if (ev.getAction() == 1) {
is_touched = false;
} else {
is_touched = true;
}
return super.onTouchEvent(ev);
}
#Override
public void computeScroll() {
super.computeScroll();
if (getZoomLevel() != last_zoom) {
// if computeScroll called before timer counts down we should drop it and start it over again
zoom_event_delay_timer.cancel();
zoom_event_delay_timer = new Timer();
zoom_event_delay_timer.schedule(new TimerTask() {
#Override
public void run() {
zoom_change_listener.onZoomChange(_this, getZoomLevel(), last_zoom);
last_zoom = getZoomLevel();
}
}, events_timeout);
}
// Send event only when map's center has changed and user stopped touching the screen
if (!last_center_pos.equals(getMapCenter()) || !is_touched) {
pan_event_delay_timer.cancel();
pan_event_delay_timer = new Timer();
pan_event_delay_timer.schedule(new TimerTask() {
#Override
public void run() {
pan_change_listener.onPanChange(_this, getMapCenter(), last_center_pos);
last_center_pos = getMapCenter();
}
}, events_timeout);
}
}
}
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:apiKey="0TqoE_fzA3Pv-m8188NwttzIKcYBzhNJsYRJkKQ"
android:id="#+id/mapview"
android:clickable="true"
android:enabled="true"
/>
</LinearLayout>
<RelativeLayout
android:layout_width="35dip"
android:layout_marginTop="300dip"
android:layout_marginLeft="270dip"
android:layout_height="70dip"
android:background="#AA000000">
<TextView android:layout_height="20dip"
android:gravity="center" android:textStyle="bold"
android:textColor="#000000" android:textSize="20dip"
android:id="#+id/zoomButton1"
android:layout_width="100dip"
android:text="+" />
<TextView android:layout_height="20dip"
android:layout_below="#id/zoomButton1"
android:textStyle="bold"
android:textColor="#000000"
android:gravity="center"
android:textSize="20dip"
android:id="#+id/zoomButton2"
android:layout_width="80dip"
android:text="-" />
</RelativeLayout>
</RelativeLayout>
MyMapActivity
public class MyMapActivity extends MapActivity {
/** Called when the activity is first created. */
MapView mapView;
ZoomControls zoomControls;
MapController mapController;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView)findViewById(R.id.mapview);
System.out.println("oncreate");
mapView.setStreetView(true);
mapView.invalidate();
mapController = mapView.getController();
// zoom contrlos
int y=10;
int x=10;
/*MapView.LayoutParams lp;
lp = new MapView.LayoutParams(MapView.LayoutParams.WRAP_CONTENT,
MapView.LayoutParams.WRAP_CONTENT,
x, y,
MapView.LayoutParams.TOP_LEFT);
View zoomControls = mapView.getZoomControls();
mapView.addView(zoomControls, lp);
mapView.displayZoomControls(true);*/
/*zoomControls = (ZoomControls) findViewById(R.id.zoomControls1);
zoomControls.setOnZoomInClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mapController.zoomIn();
}
});
zoomControls.setOnZoomOutClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mapController.zoomOut();
}
});*/
TextView zButton =(TextView)findViewById(R.id.zoomButton1);
TextView zButton1 =(TextView)findViewById(R.id.zoomButton2);
zButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mapController.zoomIn();
Toast.makeText(myMapActivity.this, "Zoomin", Toast.LENGTH_SHORT).show();
}
});
zButton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mapController.zoomOut();
Toast.makeText(myMapActivity.this, "ZoomOUT", Toast.LENGTH_SHORT).show();
}
});
Double lat = 38.899049*1E6;
Double lng = -77.017593*1E6;
GeoPoint point = new GeoPoint(lat.intValue(),lng.intValue());
System.out.println("Points"+point);
mapController.setCenter(point);
mapController.setZoom(15);
mapController.animateTo(point);
// http://maps.google.com/maps?ie=UTF8&hl=en&layer=t&ll=38.899049,-77.017593&spn=0.268261,0.6427&z=11
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
}