package com.su.keylight; /** * defy定时键盘灯工具 * @author sfshine * @blog blog.csdn.net/sfshine */ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Calendar; import android.app.Activity; import android.app.AlarmManager; import android.app.AlertDialog; import android.app.PendingIntent; import android.app.TimePickerDialog; import android.content.DialogInterface; import android.content.Intent; import android.content.res.AssetManager; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.TimePicker; import android.widget.Toast; public class KeyLightActivity extends Activity { /* 声明对象变量 */ private String TAG= "KeyLightActivity"; private String openTime; private String shutDownTime; static Calendar c = Calendar.getInstance(); private TextView textViewshutdown; private TextView textViewopen; private View buttonopen; private Button buttondelopen; private Button buttonshutdown; private Button buttondelshutdown; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); CopyAssets(); Util.RootCmd("chmod 777 " + Util.DATA_PATH + "DefyBacklightopen.sh"); Util.RootCmd("chmod 777 " + Util.DATA_PATH + "DefyBacklightSwitchshutdown.sh"); String[] s = DataStore.fetch(this); openTime = s[0]; shutDownTime = s[1]; textViewshutdown = (TextView) findViewById(R.id.textViewshutdown); textViewopen = (TextView) findViewById(R.id.textViewopen); if (null!=openTime) { textViewopen.setText(openTime); } if (null!=shutDownTime) { textViewshutdown.setText(shutDownTime); } buttonopen = (Button) findViewById(R.id.buttonopen); buttonopen.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { c.setTimeInMillis(System.currentTimeMillis()); int mHour = c.get(Calendar.HOUR_OF_DAY); int mMinute = c.get(Calendar.MINUTE); new TimePickerDialog(KeyLightActivity.this, new TimePickerDialog.OnTimeSetListener() { public void onTimeSet(TimePicker view, int hourOfDay, int minute) { c.setTimeInMillis(System.currentTimeMillis()); c.set(Calendar.HOUR_OF_DAY, hourOfDay); c.set(Calendar.MINUTE, minute); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); int day = c.get(Calendar.DAY_OF_YEAR); if (c.getTimeInMillis()<System.currentTimeMillis()) { c.set(Calendar.DAY_OF_YEAR, day+1); } Intent intent = new Intent( KeyLightActivity.this, CallOpen.class); PendingIntent sender = PendingIntent .getBroadcast(KeyLightActivity.this, 0, intent, 0); AlarmManager am; am = (AlarmManager) getSystemService(ALARM_SERVICE); am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),86400*1000,sender);//设置关闭时间 Log.i(TAG, c.getTimeInMillis()+""); openTime = format(hourOfDay) + ":" + format(minute); textViewopen.setText(openTime); DataStore.storeopentime(KeyLightActivity.this, openTime); Toast.makeText(KeyLightActivity.this, "设置开启时间为" + openTime, Toast.LENGTH_SHORT).show(); } }, mHour, mMinute, true).show(); } }); buttondelopen = (Button) findViewById(R.id.buttondelopen); buttondelopen.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(KeyLightActivity.this, CallOpen.class); PendingIntent sender = PendingIntent.getBroadcast( KeyLightActivity.this, 0, intent, 0); AlarmManager am; am = (AlarmManager) getSystemService(ALARM_SERVICE); am.cancel(sender);//取消关闭时间 DataStore.clearOpenTime(KeyLightActivity.this); Toast.makeText(KeyLightActivity.this, "开启时间解除", Toast.LENGTH_SHORT).show(); textViewopen.setText("目前无设置"); } }); buttonshutdown = (Button) findViewById(R.id.buttonshutdown); buttonshutdown.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { c.setTimeInMillis(System.currentTimeMillis()); int mHour = c.get(Calendar.HOUR_OF_DAY); int mMinute = c.get(Calendar.MINUTE); new TimePickerDialog(KeyLightActivity.this, new TimePickerDialog.OnTimeSetListener() { public void onTimeSet(TimePicker view, int hourOfDay, int minute) { c.setTimeInMillis(System.currentTimeMillis()); c.set(Calendar.HOUR_OF_DAY, hourOfDay); c.set(Calendar.MINUTE, minute); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); int day = c.get(Calendar.DAY_OF_YEAR); if (c.getTimeInMillis()<System.currentTimeMillis()) { c.set(Calendar.DAY_OF_YEAR, day+1); } Intent intent = new Intent( KeyLightActivity.this, CallShutDown.class); PendingIntent sender = PendingIntent .getBroadcast(KeyLightActivity.this, 0, intent, 0); AlarmManager am; am = (AlarmManager) getSystemService(ALARM_SERVICE); // am.set(AlarmManager.RTC_WAKEUP, // c.getTimeInMillis(), sender); am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),86400*1000,sender); /* 更新显示的设置闹钟时间 */ shutDownTime = format(hourOfDay) + ":" + format(minute); textViewshutdown.setText(shutDownTime); DataStore.storeshutdowntime( KeyLightActivity.this, shutDownTime); Toast.makeText(KeyLightActivity.this, "设置关闭时间为" + shutDownTime, Toast.LENGTH_SHORT).show(); } }, mHour, mMinute, true).show(); } }); buttondelshutdown = (Button) findViewById(R.id.buttondelshutdown); buttondelshutdown.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(KeyLightActivity.this, CallShutDown.class); PendingIntent sender = PendingIntent.getBroadcast( KeyLightActivity.this, 0, intent, 0); AlarmManager am; am = (AlarmManager) getSystemService(ALARM_SERVICE); am.cancel(sender); DataStore.clearShutDownTime(KeyLightActivity.this); Toast.makeText(KeyLightActivity.this, "关闭时间解除", Toast.LENGTH_SHORT).show(); textViewshutdown.setText("目前无设置"); } }); } /* 日期时间显示两位数的方法 */ private String format(int x) { String s = "" + x; if (s.length() == 1) s = "0" + s; return s; } private void CopyAssets() { AssetManager assetManager = getAssets(); String[] files = null; try { files = assetManager.list(""); } catch (IOException e) { } for (int i = 0; i < files.length; i++) { InputStream in = null; OutputStream out = null; try { if (!(new File(Util.DATA_PATH + files[i])).exists()) { in = assetManager.open(files[i]); out = new FileOutputStream(Util.DATA_PATH + files[i]); Util.copyFile(in, out); in.close(); in = null; out.flush(); out.close(); out = null; } } catch (Exception e) { } } } }