Animal Piano Source Code Android

ISpy
AnimalPianoActivity.java
package com.jcarx.animalpiano;


import java.io.PrintWriter;
import java.io.StringWriter;

import com.google.ads.AdRequest;
import com.google.ads.AdView;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.drawable.AnimationDrawable;
import android.location.Location;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;


public class AnimalPianoActivity extends Activity implements OnCompletionListener,  OnPreparedListener, Runnable {

    
	private ImageButton wolfb = null;
	private ImageButton lionb = null;
	private ImageButton dogb = null;
	private ImageButton catb = null;
	//private ImageButton cowb = null;
	private ImageButton roosterb = null;
	private ImageButton rateme = null;
	private ImageButton comingsoon = null;
	private VideoView vd = null;
	
	Uri motion = null;
	
	private Thread videoThread = null;
	
	private  AnimationDrawable ratemeAnimation; 
	private  AnimationDrawable comingsoonAnimation; 
	
	private Uri wolfuri = Uri.parse("android.resource://com.jcarx.animalpiano/raw/wolf");
	private Uri lionuri = Uri.parse("android.resource://com.jcarx.animalpiano/raw/lion");
	private Uri caturi = Uri.parse("android.resource://com.jcarx.animalpiano/raw/cat");
	private Uri doguri = Uri.parse("android.resource://com.jcarx.animalpiano/raw/dog");
	//private Uri cowuri = Uri.parse("android.resource://com.jcarx.animalpiano/raw/cow");
	private Uri roosteruri = Uri.parse("android.resource://com.jcarx.animalpiano/raw/rooster");
	
	private static final String TAG = "AnimalPianoActivity";
	
	private MediaPlayer mMediaPlayer;
	//private MediaPlayer mMediaPlayerwolf;
	//private MediaPlayer mMediaPlayerlion;
	//private MediaPlayer mMediaPlayercat;
	
	
    private static final String MEDIA = "media";
    private static final int LOCAL_AUDIO = 1;
    private static final int STREAM_AUDIO = 2;
    private static final int RESOURCES_AUDIO = 3;
    private static final int LOCAL_VIDEO = 4;
    private static final int STREAM_VIDEO = 5;
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        int w = getScreenWidth(this);
        int h = getScreenHeight(this);
        
        //Toast.makeText(this, "W="+w+" H="+h,Toast.LENGTH_LONG).show();
        Log.e(TAG, "W="+w+" H="+h);
        
        int dim = w+h;
        // hig resolution phone
       // w=540 h = 960 
        
        if(dim < 1501){
        	Log.e(TAG, "Phone Screen W="+w+" H="+h);
            //Toast.makeText(this, "Phone Screen Dim="+dim,Toast.LENGTH_LONG).show();
            setContentView(R.layout.phone);
        	setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        // Kindle and high resolution phones
        }else if((dim > 1501) && (dim < 1750)){
        	Log.e(TAG, "Medium Screen W="+w+" H="+h);
            //Toast.makeText(this, "Medium Screen Dim="+dim,Toast.LENGTH_LONG).show();
            setContentView(R.layout.tabletmedium);
        	setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }else {
        	Log.e(TAG, "Tablet Screen W="+w+" H="+h);
        	//Toast.makeText(this, "Tablet Screen Dim="+dim,Toast.LENGTH_LONG).show();
            setContentView(R.layout.tablet);
        	setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }

        
        wolfb = (ImageButton) findViewById(R.id.wolfb);
        lionb = (ImageButton) findViewById(R.id.lionb);
        dogb = (ImageButton) findViewById(R.id.dogb);
        catb = (ImageButton) findViewById(R.id.catb);
        rateme = (ImageButton) findViewById(R.id.ratemeb);
       // ratemeAnimation = (AnimationDrawable) rateme.getDrawable();
        comingsoon = (ImageButton) findViewById(R.id.comingsoonb);
        comingsoonAnimation = (AnimationDrawable) comingsoon.getDrawable();
        
        
    	//vd = (VideoView) findViewById(R.id.videoview);
		//motion = Uri.parse("android.resource://" + getPackageName() + "/"
		//		+ R.raw.motion);
	
		//vd.setVideoURI(motion);
		//vd.setSoundEffectsEnabled(false);
		//vd.setOnPreparedListener(this);
		//vd.setFocusable(true);
		//vd.requestFocus();
		  //Video Loop
        //vd.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        //    public void onCompletion(MediaPlayer mp) {
        //        vd.start(); //need to make transition seamless.
        //    }
        //});
		//vd.setOnCompletionListener(this);

        
        //cowb = (ImageButton) findViewById(R.id.cowb);
        roosterb = (ImageButton) findViewById(R.id.roosterb);
        
         //Determine screen size
        if ((getResources().getConfiguration().screenLayout &      Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {     
           // Toast.makeText(this, "Large screen",Toast.LENGTH_LONG).show();
            Log.e(TAG, "Large Screen");
        }
        else if ((getResources().getConfiguration().screenLayout &      Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {     
           // Toast.makeText(this, "Normal sized screen" , Toast.LENGTH_LONG).show();
            Log.e(TAG, "Normal Screen");
        } 
        else if ((getResources().getConfiguration().screenLayout &      Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {     
           // Toast.makeText(this, "Small sized screen" , Toast.LENGTH_LONG).show();
            Log.e(TAG, "Small Screen");
        }
        else {
          //  Toast.makeText(this, "Screen size is neither large, normal or small" , Toast.LENGTH_LONG).show();
            Log.e(TAG, "Screen size is neither large, normal or small");
        }
        
         	//mMediaPlayer = MediaPlayer.create(this,R.raw.lion);
    	 	//mMediaPlayerlion = MediaPlayer.create(this,lionuri);
    	 	//mMediaPlayer.setDataSource(this, lionuri);
        	//mMediaPlayer = MediaPlayer.create(this,R.raw.wolf);
    	 	//mMediaPlayerwolf = MediaPlayer.create(this,wolfuri);
        	//mMediaPlayer.setDataSource(this, wolfuri);
        	//mMediaPlayer = MediaPlayer.create(this,R.raw.cat);
        	//mMediaPlayercat = MediaPlayer.create(this,caturi);
        	//mMediaPlayer.setDataSource(this, caturi);
        
        wolfb.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				try{
					playsound("wolf");
				}catch(Exception x){
		    			x.printStackTrace();
		    	}
			}
		});
        
        lionb.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				try{
					playsound("lion");
				}catch(Exception x){
		    			x.printStackTrace();
		    	}
			}
        });
        
        dogb.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				try{
					playsound("dog");
				}catch(Exception x){
		    			x.printStackTrace();
		    	}
			}
        });
        
        catb.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				try{
					playsound("cat");
				}catch(Exception x){
		    			x.printStackTrace();
		    	}
			}
        });
        
      /* cowb.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				try{
					playsound("cow");
				}catch(Exception x){
		    			x.printStackTrace();
		    	}
			}
        });
        */
        roosterb.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				try{
					playsound("rooster");
				}catch(Exception x){
		    			x.printStackTrace();
		    	}
			}
        });
        
        SharedPreferences prefs = getSharedPreferences("ratemeanimalpiano", 0);
        final Context mycontext = AnimalPianoActivity.this;
        rateme.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				try{
					AppRater.showRateDialog(mycontext, null);
				}catch(Exception x){
		    			x.printStackTrace();
		    	}
			}
        });
        
        
        
        Location targetlocation = new Location("FIXED");
	    targetlocation.setLatitude(Double.parseDouble(getString(R.string.targetlocation_us_latitude)));
	    targetlocation.setLongitude(Double.parseDouble(getString(R.string.targetlocation_us_longitude)));
        
	    AdView mAdView = (AdView) findViewById(R.id.adViewmain);
		AdRequest adRequest = new AdRequest();
	    
	    adRequest.addKeyword("jeans");
	    adRequest.addKeyword("shoes");
	    adRequest.addKeyword("nike");
	    adRequest.addKeyword("shirt");
	    adRequest.addKeyword("kids");
	    adRequest.addKeyword("car");
	    adRequest.addKeyword("fun");
	    adRequest.addKeyword("music");
	    adRequest.addKeyword("animals");
	    
	    adRequest.setLocation(targetlocation);
	    
	    mAdView.loadAd(adRequest);
    }
    
    
    /** This callback will be invoked when the file is finished playing */
	@Override
	public void onCompletion(MediaPlayer mp) {
		this.finish();
	}
	
    /** This will start the video Thread once the video is loaded */
	@Override
	public void onPrepared(MediaPlayer vp) {
		try {
				videoThread = new Thread(this);
				videoThread.start();
		} catch (Exception x) {
				x.printStackTrace();
				StringWriter sw = new StringWriter();
			    PrintWriter pw = new PrintWriter(sw);
			    x.printStackTrace(pw);
		}
	}
    
    public void run(){
    	try {
    			vd.start();
           	} catch (Exception x) {
           		x.printStackTrace();
           		StringWriter sw = new StringWriter();
           		PrintWriter pw = new PrintWriter(sw);
           		x.printStackTrace(pw);
           	}
	}
    
    
    public static int getScreenWidth(Context context){
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        return display.getWidth();
    }
    public static int getScreenHeight(Context context){
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        return display.getHeight();
    }
        
    public void playsound(String name){
        try{
        	
        	if(mMediaPlayer != null){
        		if(mMediaPlayer.isPlaying()){
        			mMediaPlayer.stop();
        			mMediaPlayer.reset();
        		}
        	}
        	
        	Log.w(TAG,"User wants to play ="+name);
        	
        	
        	if(name.equalsIgnoreCase("wolf")){
        		mMediaPlayer = MediaPlayer.create(this,wolfuri);
        		playaudio(mMediaPlayer);
        	}else if(name.equalsIgnoreCase("lion")){
        		mMediaPlayer = MediaPlayer.create(this,lionuri);
        		playaudio(mMediaPlayer);
            }else if(name.equalsIgnoreCase("cat")){
        		mMediaPlayer = MediaPlayer.create(this,caturi);
        		playaudio(mMediaPlayer);
            }else if(name.equalsIgnoreCase("dog")){
        	 	//mMediaPlayer = MediaPlayer.create(this,R.raw.dog);
        	 	mMediaPlayer = MediaPlayer.create(this,doguri);
        		playaudio(mMediaPlayer);
                //mMediaPlayer.setDataSource(this, doguri);
            }else if(name.equalsIgnoreCase("cow")){
            	//mMediaPlayer = MediaPlayer.create(this,R.raw.cow);
            	//mMediaPlayer = MediaPlayer.create(this,cowuri);
            	//mMediaPlayer.setDataSource(this, cowuri);
            }else if(name.equalsIgnoreCase("rooster")){
            	//mMediaPlayer = MediaPlayer.create(this,R.raw.rooster);
            	mMediaPlayer = MediaPlayer.create(this,roosteruri);
        		playaudio(mMediaPlayer);
                //mMediaPlayer.setDataSource(this, roosteruri);
            }
        	
        }catch(Exception x){
			x.printStackTrace();
			Log.e(TAG," Exception while palying sound "+name +" Error="+ x.getMessage());
		}
    }
    
    public void playaudio(MediaPlayer player){
    	if(player != null){
    		boolean noexceptions = true;
    		try{
    			Thread.sleep(300);
    			//player.prepare();
    		}catch(Exception x){
    			x.printStackTrace();
    			Log.w(TAG,"Exception could not prepare sound",x);
    			noexceptions = false;
    		}
    		
    		if(noexceptions){
    			player.setLooping(false);
        		// max volume
        		player.setVolume(1f,1f);
        		player.start();
    		}
    	}
    }

    public boolean isstillplayingSound(MediaPlayer mpl){
    	boolean status = false;
    	try{
    		int currentlifeduration = mpl.getDuration();
    		Log.w(TAG,"Current file playing duration "+currentlifeduration);
    		
    		//Thread.sleep(currentlifeduration);
    		
    		//Thread.sleep(currentlifeduration);
    		status = mpl.isPlaying();
    	}catch(Exception x){
    		x.printStackTrace();
    	}
    	return status; 
	}
    
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        //ratemeAnimation.start();
        comingsoonAnimation.start();
    }
    
    protected void onDestroy(){		
		Log.v(TAG, "onDestroy");
		super.onDestroy();
	    if (mMediaPlayer != null) {
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
	}
}
AppRater.java
package com.jcarx.animalpiano;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AppRater {
	
	private final static String APP_TITLE = "AnimalPiano";
    private final static String APP_PNAME = "com.jcarx.animalpiano";
    
    private final static int DAYS_UNTIL_PROMPT = 3;
    private final static int LAUNCHES_UNTIL_PROMPT = 7;
    
    public static void app_launched(Context mContext) {
        SharedPreferences prefs = mContext.getSharedPreferences("apprater", 0);
        if (prefs.getBoolean("dontshowagain", false)) { return ; }
        
        SharedPreferences.Editor editor = prefs.edit();
        
        // Increment launch counter
        long launch_count = prefs.getLong("launch_count", 0) + 1;
        editor.putLong("launch_count", launch_count);

        // Get date of first launch
        Long date_firstLaunch = prefs.getLong("date_firstlaunch", 0);
        if (date_firstLaunch == 0) {
            date_firstLaunch = System.currentTimeMillis();
            editor.putLong("date_firstlaunch", date_firstLaunch);
        }
        
        
        editor.commit();
    }   
    
    public static void showRateDialog(final Context mContext, final SharedPreferences.Editor editor) {
        final Dialog dialog = new Dialog(mContext);
        dialog.setTitle("Rate " + APP_TITLE);

        LinearLayout ll = new LinearLayout(mContext);
        ll.setOrientation(LinearLayout.VERTICAL);
        
        TextView tv = new TextView(mContext);
        tv.setText("If you enjoy using " + APP_TITLE + ", please take a moment to rate it. Thanks for your support!");
        tv.setWidth(240);
        tv.setPadding(4, 0, 4, 10);
        ll.addView(tv);
        
        Button b1 = new Button(mContext);
        b1.setText("Rate " + APP_TITLE);
        b1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + APP_PNAME)));
                dialog.dismiss();
            }
        });        
        ll.addView(b1);

        Button b2 = new Button(mContext);
        b2.setText("Remind me later");
        b2.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        ll.addView(b2);

        Button b3 = new Button(mContext);
        b3.setText("No, thanks");
        b3.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (editor != null) {
                    editor.putBoolean("dontshowagain", true);
                    editor.commit();
                }
                dialog.dismiss();
            }
        });
        ll.addView(b3);

        dialog.setContentView(ll);        
        dialog.show();        
    }
}
// see http://androidsnippets.com/prompt-engaged-users-to-rate-your-app-in-the-android-market-appirater

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.