Back to project page AndroidRandomWallpaper.
The source code is released under:
GNU General Public License
If you think the Android project AndroidRandomWallpaper 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.elbauldelprogramador.randomwallpaper.activities; /*from ww w .j a v a 2 s. c om*/ import java.util.List; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.net.Uri; import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.os.Environment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.util.DisplayMetrics; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.Window; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; import com.elbauldelprogramador.randomwallpaper.R; import com.elbauldelprogramador.randomwallpaper.reveivers.AlarmReceiver; import com.elbauldelprogramador.randomwallpaper.util.RWGlobal; import com.elbauldelprogramador.randomwallpaper.util.SimpleFileExplorer; import com.elbauldelprogramador.randomwallpaper.util.StrictModeWrapper; public class MainActivity extends FragmentActivity { public final static String TAG = "RandomWallPaper"; /* Views */ private EditText et_interval; private Button bt_start; private CheckBox cb_onBoot; /* Options */ private int interval; private String sourceFolder; private int width; private int height; private boolean startAtBoot; private SharedPreferences sharedPref; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Quitar el ttulo si la versin de Android es inferior a la 2.3.3 if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); try { StrictModeWrapper.init(this); } catch (Throwable throwable) { Log.v("StrictMode", "... is not available. Punting..."); } bt_start = (Button) findViewById(R.id.button_change_wall); et_interval = (EditText) findViewById(R.id.editText_interval); cb_onBoot = (CheckBox) findViewById(R.id.checkBox_launchOnBoot); /* Retrieve stored preferences */ sharedPref = getSharedPreferences(RWGlobal.SHARED_PREFERENCES, Context.MODE_PRIVATE); interval = sharedPref.getInt(RWGlobal.KEY_INTERVAL, RWGlobal.DEFAULT_INTERVAL); sourceFolder = sharedPref.getString(RWGlobal.KEY_SOURCE_FOLDER, RWGlobal.DEFAULT_SOURCE_FOLDER); height = sharedPref.getInt(RWGlobal.KEY_HEGHT, RWGlobal.DEFAULT_HEIGHT); width = sharedPref.getInt(RWGlobal.KEY_WIDTH, RWGlobal.DEFAULT_WIDTH); startAtBoot = sharedPref.getBoolean(RWGlobal.KEY_AT_BOOT, RWGlobal.DEFAULT_AT_BOOT); DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); /* Setting up Views */ et_interval.setText(String.valueOf(interval)); if(RWGlobal.thereAreImages(sourceFolder)) bt_start.setEnabled(true); cb_onBoot.setChecked(startAtBoot); if (height == RWGlobal.DEFAULT_HEIGHT) height = dm.heightPixels; if (width == RWGlobal.DEFAULT_WIDTH) width = dm.widthPixels << 1; // best wallpaper width is twice screen width Log.d(MainActivity.TAG, "in OnCreate() Updating sourceFolder to: " + sourceFolder); Log.d(MainActivity.TAG, "in OnCreate() Updating interval to: " + interval); Log.d(MainActivity.TAG, "in OnCreate() Updating wid to: " + width); Log.d(MainActivity.TAG, "in OnCreate() Updating he to: " + height); Log.d(MainActivity.TAG, "in OnCreate() Updating sab to: " + startAtBoot); RWGlobal.toast(this, getResources().getString(R.string.initial_message)); } public void doClick(View v){ switch (v.getId()) { case R.id.button_pick_folder: doPick(); break; case R.id.button_change_wall: scheduleChange(); break; case R.id.button_stop: stopService(); break; case R.id.checkBox_launchOnBoot: toggleLaunch(); break; default: break; } } private void doPick(){ Intent i = new Intent(Intent.ACTION_PICK); i.setData(Uri.parse("folder://" + Environment.getExternalStorageDirectory() .getPath())); //default folder / jump direcly to this folder PackageManager pm = getPackageManager(); List<ResolveInfo> list = pm.queryIntentActivities(i, 0); if (list.isEmpty()){ Log.w(MainActivity.TAG, "Not activity for pick folder, starting inner app"); i = new Intent(this, SimpleFileExplorer.class); startActivityForResult(i, RWGlobal.PICK_FOLDER); } else startActivityForResult(i, RWGlobal.PICK_FOLDER); } private void scheduleChange(){ try{ AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(this, AlarmReceiver.class); intent.putExtra(AlarmReceiver.ACTION_CHANGE_WALL_ALARM, AlarmReceiver.ACTION_CHANGE_WALL_ALARM); intent.putExtra(RWGlobal.EXTRA_PATH, sourceFolder); intent.putExtra(RWGlobal.EXTRA_HEIGHT, height); intent.putExtra(RWGlobal.EXTRA_WIDTH, width); final PendingIntent pIntent = PendingIntent. getBroadcast(this, 1234567, intent, PendingIntent.FLAG_UPDATE_CURRENT); if (!et_interval.getText().toString().isEmpty()) interval = Integer.parseInt(et_interval.getText().toString()); alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval * 60000, pIntent); RWGlobal.toast(this, "Started..."); } catch(Exception e){ e.printStackTrace(); } } private void stopService() { Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class); intent.putExtra(AlarmReceiver.ACTION_CHANGE_WALL_ALARM, AlarmReceiver.ACTION_CHANGE_WALL_ALARM); final PendingIntent pIntent = PendingIntent.getBroadcast(this, 1234567, intent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE); alarms.cancel(pIntent); RWGlobal.toast(this, "Canceled..."); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { //This is to inform the parent class (Activity) //that the called activity has finished and the baseclass //can do the necessary clean up super.onActivityResult(requestCode, resultCode, intent); switch (requestCode) { case RWGlobal.PICK_FOLDER: switch (resultCode) { case RESULT_OK: Log.d(MainActivity.TAG, intent.getData().getPath().toString()); sourceFolder = intent.getData().getPath().toString(); bt_start.setEnabled(true); /* Now it is possible start the service */ break; case RWGlobal.RESULT_NO_FILES: Toast.makeText(this, "No Files", Toast.LENGTH_LONG).show(); break; case RWGlobal.RESULT_NO_PATH: Toast.makeText(this, "No PATH", Toast.LENGTH_LONG).show(); break; } break; default: break; } } private void toggleLaunch() { startAtBoot = cb_onBoot.isChecked() ? true : false; cb_onBoot.setChecked(startAtBoot); } @Override protected void onStop() { super.onStop(); new SavePreferences().execute(); } @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; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_about: showAboutDialog(); break; default: break; } return super.onOptionsItemSelected(item); } private void showAboutDialog() { FragmentManager fm = getSupportFragmentManager(); AboutTheApp dialog = new AboutTheApp(); dialog.show(fm, "ESTE TAG ARAGQUE ES"); } /** * Write to preferences file in other thread */ private class SavePreferences extends AsyncTask<Void, Void, Void>{ @Override protected Void doInBackground(Void... params) { SharedPreferences settings = getSharedPreferences(RWGlobal.SHARED_PREFERENCES, Context.MODE_PRIVATE); SharedPreferences.Editor editor = settings.edit(); editor.putString(RWGlobal.KEY_SOURCE_FOLDER, sourceFolder); editor.putInt(RWGlobal.KEY_INTERVAL, interval); editor.putInt(RWGlobal.KEY_HEGHT, height); editor.putInt(RWGlobal.KEY_WIDTH, width); editor.putBoolean(RWGlobal.KEY_AT_BOOT, startAtBoot); editor.commit(); Log.i(MainActivity.TAG, "Updating sourceFolder to: " + sourceFolder); Log.i(MainActivity.TAG, "Updating interval to: " + interval); Log.i(MainActivity.TAG, "Updating heght to: " + height); Log.i(MainActivity.TAG, "Updating width to: " + width); Log.i(MainActivity.TAG, "Updating startAtBoot to: " + startAtBoot); return null; } } }