Android Open Source - PocketLantern Main Activity






From Project

Back to project page PocketLantern.

License

The source code is released under:

GNU General Public License

If you think the Android project PocketLantern 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 cc.co.techzealous.pl;
/*  w  w  w  . ja  v a  2 s .c  o  m*/
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Shader.TileMode;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import cc.co.techzealous.pl.utils.MyView;
import cc.co.techzealous.pl.utils.PLConstants;

public class MainActivity extends Activity {

  private SharedPreferences prefs;
  private MyView myViewPl;
  
  private int screenWidth;
  private int screenHeight;
  private Paint paintTemp;
  private RadialGradient rg;
  private boolean isLit;
  
  private Animation anim;
  private Handler myHandler;
  private Runnable myRunnable;
  private boolean lighting;
  private Thread myThread;
  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        prefs = PreferenceManager.getDefaultSharedPreferences(this);
        myViewPl = (MyView) findViewById(R.id.myViewMain);
        anim = AnimationUtils.loadAnimation(this, R.anim.fadeout);
        paintTemp = new Paint(Paint.ANTI_ALIAS_FLAG);
        myHandler = new Handler();
        myRunnable = new Runnable() {
          public void run() {
            myViewPl.startAnimation(anim);
            
            myHandler.removeCallbacks(myRunnable);
            
            if(lighting) {
              myHandler.postDelayed(myRunnable, 150);
            }
          }
        };
        
        checkEula();
    }
    
    @Override
    public void onPause() {
      super.onPause();
      lighting = false;
      isLit = false;
    }
    
    @Override
    public void onResume() {
      super.onResume();
      
      makeLight();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
      switch(item.getItemId()) {
      case R.id.menu_settings:
        Intent i = new Intent(MainActivity.this, PLPreferenceActivity.class);
      startActivity(i);
        return true;
      case R.id.menu_light:
        makeLight();
        return true;
      case R.id.menu_more:
        Intent i2 = new Intent(Intent.ACTION_VIEW, Uri.parse(PLConstants.MY_STORE_PAGE));
        startActivity(i2);
        return true;
      }
      return false;
    }
    
    private void makeLight() {
      if(!isLit) {
      isLit = true;
      if(!prefs.contains("x")) {
        screenWidth = getWindowManager().getDefaultDisplay().getWidth();
        screenHeight = getWindowManager().getDefaultDisplay().getHeight();
        int width = screenWidth / 2;
        int height = screenHeight / 2 + 10;
        prefs.edit().putInt(PLConstants.PREF_POSX, width).commit();
        prefs.edit().putInt(PLConstants.PREF_POSY, height).commit();
      }
      rg = new RadialGradient(prefs.getInt(PLConstants.PREF_POSX, 0), prefs.getInt(PLConstants.PREF_POSY, 0), prefs.getInt(PLConstants.PREF_SIZE, PLConstants.DEFAULT_SIZE), Integer.parseInt(prefs.getString(getResources().getString(R.string.pref_key_color), PLConstants.PREF_COLOR_GREEN)), Color.TRANSPARENT, TileMode.CLAMP);
      paintTemp.setAlpha(255);
      paintTemp.setShader(rg);
      myViewPl.setLightPaint(paintTemp);
    
      myViewPl.setPosX(prefs.getInt(PLConstants.PREF_POSX, 0));
      myViewPl.setPosY(prefs.getInt(PLConstants.PREF_POSY, 0));
      myViewPl.setSize(prefs.getInt(PLConstants.PREF_SIZE, prefs.getInt(PLConstants.PREF_SIZE, PLConstants.DEFAULT_SIZE)));
      myViewPl.invalidate();
      
      lighting = true;
      if(prefs.getBoolean(getResources().getString(R.string.pref_key_pulsate), true)) {
        myThread = new Thread(new Runnable() {
        public void run() {
          myHandler.post(myRunnable);
        }
        });
        myThread.start();
      }
      
    } else {
      lighting = false;
      isLit = false;
      paintTemp.setAlpha(0);
      myViewPl.setLightPaint(paintTemp);
      myViewPl.invalidate();
    }
    }
    
    private void checkEula() {
      if(!prefs.getBoolean(PLConstants.PREF_EULA, false)) {
        Intent e = new Intent(MainActivity.this, PLEulaActivity.class);
        startActivity(e);
        finish();
      }
    }
}




Java Source Code List

cc.co.techzealous.pl.MainActivity.java
cc.co.techzealous.pl.PLEulaActivity.java
cc.co.techzealous.pl.PLPreferenceActivity.java
cc.co.techzealous.pl.PLSizeActivity.java
cc.co.techzealous.pl.utils.MyView.java
cc.co.techzealous.pl.utils.PLConstants.java