Android Open Source - TacoTool Main Activity






From Project

Back to project page TacoTool.

License

The source code is released under:

GNU General Public License

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

Java Source Code

package com.example.tacotool;
//  www.j  ava2  s .  c  o m
import java.util.Calendar;
import java.util.Locale;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

@SuppressLint("NewApi")
public class MainActivity extends Activity {
  MediaPlayer mediaPlayer;
  boolean musicOn;
  SharedPreferences sharedPref;
  String tTime;
  int[] track;
  //tvungen att spara lyssnaren s hr annars slnger garbage collectorn
  //refen
  private OnSharedPreferenceChangeListener changeListener;
  
  @SuppressLint("NewApi")
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    
    

    
    
    
    track = new int[2];
    track[0] = R.raw.manana;
    track[1] = R.raw.glow;
    mediaPlayer = MediaPlayer.create(this, R.raw.manana);
    sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    ActionBar ab = getActionBar();
    ab.setTitle(R.string.app_name);
    changeListener = new OnSharedPreferenceChangeListener() {
      
      @Override
      public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
          String key) {
        
        if (key.equals("sound_on")) {
          
          if (sharedPreferences.getBoolean("sound_on", false)) {
            //kanske kan kolla om musiken r startad s slipper man pausa
            mediaPlayer.start();
            mediaPlayer.setLooping(true);
          } else if (mediaPlayer.isPlaying()){
            mediaPlayer.stop();
            mediaPlayer.reset();
          }
        }
        
        if (key.equals("taco_theme")) {
          
          if (mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
            mediaPlayer.reset();
          }
          
          
          String selected = sharedPreferences.getString("selected_taco_theme", "manana");
          if (selected.equals("manana")) {
            mediaPlayer = MediaPlayer.create(getApplicationContext(), track[0]);
          } else if (selected.equals("cha cha cha")) {
            mediaPlayer = MediaPlayer.create(getApplicationContext(), track[1]);
          }
          onSharedPreferenceChanged(sharedPreferences, "sound_on");
        }
        
      
        
      }

    };
    
    
    
    sharedPref.registerOnSharedPreferenceChangeListener(changeListener);
    

    if (sharedPref.getBoolean("sound_on", false)) {
      mediaPlayer.start();
    }
    
  }

  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }



  public boolean onOptionsItemSelected(MenuItem item) {
    Intent intent;
    switch (item.getItemId()) {

//    case 1: 
//      intent = new Intent(this, CreateActivity.class);
//      break;
    default:
      intent = new Intent(this, SettingsActivity.class);
      break;
    }
    startActivity(intent);

    return false;
  }
  public void onBackPressed(){
    super.onBackPressed();
    mediaPlayer.stop();
    mediaPlayer.release();
  }
  
  
  //responds to onclick:
  
  public void startCreate(View view) {
    Intent intent = new Intent(this, CreateActivity.class);
    startActivity(intent);
  }
  
  public void startManage(View view) {
    Intent intent = new Intent(this, ManageActivity.class);
    startActivity(intent);
  }
  
  public void startTacoTime(View view) {
//    Intent intent = new Intent(this, TacoTimeActivity.class);
//    startActivity(intent);
    Calendar cal = Calendar.getInstance();
    cal.getTime().toString();
    
    String s = cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.getDefault());

    int time = cal.get(Calendar.HOUR_OF_DAY);
    
    tTime = "Yes, time for TACO!!";
    if (s.equals("Fri") || s.equals("FRI") || s.equals("fri")) {
      tTime = "YES, time for TACO and TEQUILA!!";
    }
    
    if (time == 11 || time == 16) {
      tTime = "No, it is coffee time";
    }
    
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(R.string.t_tacotime).setMessage(tTime);
    

    AlertDialog dialog = builder.create();
    dialog.show();
    
    
  }
  
}




Java Source Code List

com.example.tacotool.CreateActivity.java
com.example.tacotool.MainActivity.java
com.example.tacotool.ManageActivity.java
com.example.tacotool.SettingsActivity.java
com.example.tacotool.Taco.java
database.DBManager.java
database.DBSQLiteHelper.java