Back to project page PureLight.
The source code is released under:
GNU General Public License
If you think the Android project PureLight listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.lzuoss.purelight; //from www.j a va 2 s. c o m import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.app.DialogFragment; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.FeatureInfo; import android.content.pm.PackageManager; import android.hardware.Camera; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.view.WindowManager; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.Switch; import android.widget.Toast; public class MainActivity extends Activity { boolean isAutoLightOn; boolean hasFlashLight; boolean isLightOn; boolean isClassic; boolean isTrick; boolean isBroken; int isHint; int totalTime; long currentThreadOn; Camera camera=Camera.open(); TimerHander timerHander; private SensorManager mSensorManager; private float mAccel; private float mAccelCurrent; private float mAccelLast; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageButton settings = (ImageButton) findViewById(R.id.imageButton1); ImageButton timer = (ImageButton) findViewById(R.id.imageButton2); settings.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { ImageButton settings = (ImageButton) findViewById(R.id.imageButton1); if(event.getAction() == MotionEvent.ACTION_DOWN){ settings.setImageResource(R.drawable.setting_click); }else if(event.getAction() == MotionEvent.ACTION_UP){ settings.setImageResource(R.drawable.setting); } return false; } }); timer.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { ImageButton timer= (ImageButton) findViewById(R.id.imageButton2); if(event.getAction() == MotionEvent.ACTION_DOWN){ timer.setImageResource(R.drawable.timer_click); }else if(event.getAction() == MotionEvent.ACTION_UP){ timer.setImageResource(R.drawable.timer); } return false; } }); isAutoLightOn = false; isLightOn = false; isClassic = false; isTrick = false; isBroken = false; currentThreadOn = 0; isHint = 0; isBroken = true; SharedPreferences sharedPreferences = getSharedPreferences("DATA", Context.MODE_PRIVATE); isClassic = sharedPreferences.getBoolean("isClassic", false); isTrick = sharedPreferences.getBoolean("isTrick", false); isAutoLightOn = sharedPreferences.getBoolean("isAutoLightOn", false); totalTime = 1440; hasFlashLight = false; FeatureInfo[] feature=MainActivity.this.getPackageManager().getSystemAvailableFeatures(); for (FeatureInfo featureInfo : feature) { if (PackageManager.FEATURE_CAMERA_FLASH.equals(featureInfo.name)) { hasFlashLight=true; break; } } if (hasFlashLight == false) { Toast.makeText(this, R.string.no_flashlight, Toast.LENGTH_LONG).show(); } getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); timerHander = new TimerHander(); mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL); mAccel = 0.00f; mAccelCurrent = SensorManager.GRAVITY_EARTH; mAccelLast = SensorManager.GRAVITY_EARTH; if (isAutoLightOn == true) {turnOnLight(); isLightOn = true;} updateUI(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { Intent intent = new Intent(this, AboutActivity.class); startActivity(intent); return super.onOptionsItemSelected(item); } public void settingsClick(View v) { ChooseDialog chooseDialog = new ChooseDialog(); chooseDialog.show(getFragmentManager(), ""); } public void timerClick(View v) { TimerDialog timerDialog = new TimerDialog(); timerDialog.show(getFragmentManager(), ""); } public void switchClick(View v) { if (isLightOn == false) { if (isClassic == true && isTrick == true &&isBroken == true) { if (isHint++>=3) { Toast.makeText(MainActivity.this, R.string.shake, Toast.LENGTH_SHORT).show(); } }else { isLightOn = true; updateUI(); turnOnLight(); } }else if (isLightOn == true){ isLightOn = false; updateUI(); turnOffLight(); } } @Override protected void onPause() { if (isLightOn == true) { turnOffLight(); updateUI(); } mSensorManager.unregisterListener(mSensorListener); super.onPause(); } @Override protected void onResume() { if (isLightOn == true) { turnOnLight(); updateUI(); } super.onResume(); mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL); } @Override protected void onDestroy() { System.exit(0); } private void turnOnLight() { if (hasFlashLight) { Camera.Parameters parameters=camera.getParameters(); parameters.setFlashMode(Camera.Parameters.FLASH_MODE_ON); parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); camera.setParameters(parameters); }else { Toast.makeText(this, R.string.no_flashlight, Toast.LENGTH_LONG).show(); } } private void turnOffLight() { if (hasFlashLight) { Camera.Parameters parameters=camera.getParameters(); parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF); camera.setParameters(parameters); }else{ Toast.makeText(this, R.string.no_flashlight, Toast.LENGTH_LONG).show(); } } private void updateUI() { ImageView imageView = (ImageView) MainActivity.this.findViewById(R.id.ImageView1); if (isClassic == false) { if (isLightOn == false) { imageView.setImageResource(R.drawable.light_off); }else if (isLightOn == true){ imageView.setImageResource(R.drawable.light_on); } } else if (isClassic == true) { if (isLightOn == false) { imageView.setImageResource(R.drawable.old_light_off); }else if (isLightOn == true){ imageView.setImageResource(R.drawable.old_light_on); } } } private class TimerDialog extends DialogFragment{ @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(R.string.set_timer) .setItems(R.array.timers_array, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { switch (which) { case 0: totalTime = 1; break; case 1: totalTime = 2; break; case 2: totalTime = 5; break; case 3: totalTime = 7; break; case 4: totalTime = 10; break; case 5: totalTime = 15; break; case 6: totalTime = 1440; break; default: break; } if (totalTime == 1440) { Toast.makeText(MainActivity.this, R.string.cancel, Toast.LENGTH_SHORT).show(); }else { Toast.makeText(MainActivity.this, MainActivity.this.getString(R.string.timer_info1) + " " +totalTime + " " + MainActivity.this.getString(R.string.timer_info2), Toast.LENGTH_SHORT).show(); } Thread abc = new Thread(new TimerToExit()); abc.start(); currentThreadOn = abc.getId(); } }); return builder.create(); } } private class ChooseDialog extends DialogFragment{ @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(R.string.set_style); LayoutInflater inflater = getActivity().getLayoutInflater(); View view = inflater.inflate(R.layout.activity_choose, null); builder.setView(view); Switch isSwitch = (Switch) view.findViewById(R.id.switch1); CheckBox checkBox = (CheckBox)view.findViewById(R.id.checkBox1); CheckBox checkBox2 = (CheckBox)view.findViewById(R.id.checkBox2); isSwitch.setChecked(isClassic); checkBox.setChecked(isTrick); checkBox2.setChecked(isAutoLightOn); isSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { SharedPreferences.Editor editor = MainActivity.this.getSharedPreferences("DATA", Context.MODE_PRIVATE).edit(); editor.putBoolean("isClassic", isChecked); editor.commit(); isClassic = isChecked; MainActivity.this.updateUI(); } }); checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { SharedPreferences.Editor editor = MainActivity.this.getSharedPreferences("DATA", Context.MODE_PRIVATE).edit(); editor.putBoolean("isTrick", isChecked); editor.commit(); isTrick = isChecked; } }); checkBox2.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { SharedPreferences.Editor editor = MainActivity.this.getSharedPreferences("DATA", Context.MODE_PRIVATE).edit(); editor.putBoolean("isAutoLightOn", isChecked); editor.commit(); isAutoLightOn = isChecked; } }); return builder.create(); } } private class TimerHander extends Handler{ @Override public void handleMessage(Message msg) { super.handleMessage(msg); Bundle message = msg.getData(); String command = message.getString("COMMAND"); long threadID = message.getLong("ID"); if (command.equals("EXIT") && threadID == currentThreadOn) { System.exit(0); } } } private class TimerToExit implements Runnable{ @Override public void run() { try { Thread.sleep(totalTime*1000*60); } catch (InterruptedException e) { e.printStackTrace(); } Bundle data = new Bundle(); data.putString("COMMAND", "EXIT"); data.putLong("ID", Thread.currentThread().getId()); Message message = new Message(); message.setData(data); MainActivity.this.timerHander.sendMessage(message); } } private final SensorEventListener mSensorListener = new SensorEventListener() { public void onSensorChanged(SensorEvent se) { float x = se.values[0]; float y = se.values[1]; float z = se.values[2]; mAccelLast = mAccelCurrent; mAccelCurrent = (float) Math.sqrt((double) (x*x + y*y + z*z)); float delta = mAccelCurrent - mAccelLast; mAccel = mAccel * 0.9f + delta; if (mAccel > 2) isBroken = false; } public void onAccuracyChanged(Sensor sensor, int accuracy) { } }; }