Android Open Source - yammp Sleep Timer Dialog






From Project

Back to project page yammp.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project yammp 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

/*
 *              Copyright (C) 2011 The MusicMod Project
 */*w ww .  j  ava  2 s .c o m*/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *            http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.yammp.dialog;

import org.yammp.Constants;
import org.yammp.R;
import org.yammp.util.MusicUtils;
import org.yammp.util.PreferencesEditor;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.media.AudioManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.DisplayMetrics;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class SleepTimerDialog extends FragmentActivity implements OnSeekBarChangeListener,
    Constants {

  private SeekBar mSetTime;
  private TextView mTimeView;
  private String mPrompt;
  private int mProgress, mTimerTime, mRemained;

  private AlertDialog mSleepTimerDialog;

  private String action;

  @Override
  public void onCreate(Bundle icicle) {

    super.onCreate(icicle);

    setContentView(new LinearLayout(this));

    DisplayMetrics dm = new DisplayMetrics();
    dm = getResources().getDisplayMetrics();

    action = getIntent().getAction();

    mSleepTimerDialog = new AlertDialog.Builder(this).create();
    mSleepTimerDialog.setVolumeControlStream(AudioManager.STREAM_MUSIC);

    mRemained = (int) MusicUtils.getSleepTimerRemained() / 1000 / 60;

    LinearLayout mContainer = new LinearLayout(this);
    mContainer.setOrientation(LinearLayout.VERTICAL);
    mContainer.setPadding((int) dm.density * 8, 0, (int) dm.density * 8, 0);
    mTimeView = new TextView(this);
    mContainer.addView(mTimeView);
    mSetTime = new SeekBar(this);
    mSetTime.setMax(120);
    mContainer.addView(mSetTime);

    if (mRemained > 0) {
      mSetTime.setProgress(mRemained);
    } else {
      mSetTime.setProgress(30);
    }

    mSetTime.setOnSeekBarChangeListener(this);

    mProgress = mSetTime.getProgress();
    mTimerTime = mProgress;
    if (mTimerTime >= 1) {
      mPrompt = SleepTimerDialog.this.getResources().getQuantityString(R.plurals.NNNminutes,
          mTimerTime, mTimerTime);
    } else {
      mPrompt = SleepTimerDialog.this.getResources().getString(R.string.disabled);
    }
    mTimeView.setText(mPrompt);

    if (INTENT_SLEEP_TIMER.equals(action)) {
      mSleepTimerDialog.setIcon(android.R.drawable.ic_dialog_info);
      mSleepTimerDialog.setTitle(R.string.set_time);
      mSleepTimerDialog.setView(mContainer);
      mSleepTimerDialog.setButton(Dialog.BUTTON_POSITIVE, getString(android.R.string.ok),
          new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

              if (mTimerTime >= 1) {
                long milliseconds = mTimerTime * 60 * 1000;
                boolean gentle = new PreferencesEditor(getApplicationContext())
                    .getBooleanPref(KEY_GENTLE_SLEEPTIMER, true);
                MusicUtils.startSleepTimer(milliseconds, gentle);
              } else {
                MusicUtils.stopSleepTimer();
              }
              finish();
            }
          });
      mSleepTimerDialog.setButton(Dialog.BUTTON_NEGATIVE, getString(android.R.string.cancel),
          new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

              finish();
            }
          });
      mSleepTimerDialog.setOnCancelListener(new OnCancelListener() {

        @Override
        public void onCancel(DialogInterface dialog) {

          finish();
        }
      });
    } else {
      Toast.makeText(this, R.string.error_bad_parameters, Toast.LENGTH_SHORT).show();
      finish();
    }
  }

  @Override
  public void onPause() {

    if (mSleepTimerDialog != null && mSleepTimerDialog.isShowing()) {
      mSleepTimerDialog.dismiss();
    }
    super.onPause();
  }

  @Override
  public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

    mTimerTime = progress;
    if (progress >= 1) {
      mPrompt = SleepTimerDialog.this.getResources().getQuantityString(R.plurals.NNNminutes,
          mTimerTime, mTimerTime);
    } else {
      mPrompt = SleepTimerDialog.this.getResources().getString(R.string.disabled);
    }
    mTimeView.setText(mPrompt);
  }

  @Override
  public void onStartTrackingTouch(SeekBar seekBar) {

  }

  @Override
  public void onStopTrackingTouch(SeekBar seekBar) {

  }

  @Override
  protected void onResume() {

    super.onResume();
    if (mSleepTimerDialog != null && !mSleepTimerDialog.isShowing()) {
      mSleepTimerDialog.show();
    }
  }

}




Java Source Code List

org.yammp.Constants.java
org.yammp.MediaAppWidgetProvider4x1.java
org.yammp.MediaAppWidgetProvider4x2.java
org.yammp.MediaButtonIntentReceiver.java
org.yammp.MusicPlaybackService.java
org.yammp.app.AlbumFragment.java
org.yammp.app.AppearanceSettingsActivity.java
org.yammp.app.ArtistFragment.java
org.yammp.app.Equalizer.java
org.yammp.app.GenreFragment.java
org.yammp.app.LyricsFragment.java
org.yammp.app.MusicBrowserActivity.java
org.yammp.app.MusicBrowserFragment.java
org.yammp.app.MusicPlaybackActivity.java
org.yammp.app.MusicSettingsActivity.java
org.yammp.app.PlaylistFragment.java
org.yammp.app.PluginFragment.java
org.yammp.app.PluginsManagerActivity.java
org.yammp.app.QueryBrowserActivity.java
org.yammp.app.QueryFragment.java
org.yammp.app.TrackBrowserActivity.java
org.yammp.app.TrackFragment.java
org.yammp.dialog.DeleteDialog.java
org.yammp.dialog.PlayShortcut.java
org.yammp.dialog.PlaylistDialog.java
org.yammp.dialog.PlaylistPickerDialog.java
org.yammp.dialog.PlaylistPicker.java
org.yammp.dialog.ScanningProgress.java
org.yammp.dialog.SearchDialog.java
org.yammp.dialog.SleepTimerDialog.java
org.yammp.dialog.VerticalTextSpinnerDialog.java
org.yammp.dialog.WeekSelector.java
org.yammp.util.ColorAnalyser.java
org.yammp.util.EqualizerWrapper.java
org.yammp.util.ImageDownloader.java
org.yammp.util.LazyImageLoader.java
org.yammp.util.LyricsDownloader.java
org.yammp.util.LyricsParser.java
org.yammp.util.LyricsSplitter.java
org.yammp.util.MusicUtils.java
org.yammp.util.PreferencesEditor.java
org.yammp.util.ServiceToken.java
org.yammp.util.ShakeListener.java
org.yammp.util.SortCursor.java
org.yammp.util.VisualizerCompatAudioFX.java
org.yammp.util.VisualizerCompatScoop.java
org.yammp.util.VisualizerCompat.java
org.yammp.util.VisualizerWrapper.java
org.yammp.view.EqualizerView.java
org.yammp.view.SliderView.java
org.yammp.view.TouchPaintView.java
org.yammp.view.VerticalTextSpinner.java
org.yammp.view.VisualizerViewFftSpectrum.java
org.yammp.view.VisualizerViewWaveForm.java
org.yammp.widget.CheckableRelativeLayout.java
org.yammp.widget.RepeatingImageButton.java
org.yammp.widget.SeparatedListAdapter.java
org.yammp.widget.TextScrollView.java
org.yammp.widget.TouchInterceptor.java