za.co.neilson.alarm.preferences.AlarmPreferencesActivity.java Source code

Java tutorial

Introduction

Here is the source code for za.co.neilson.alarm.preferences.AlarmPreferencesActivity.java

Source

/* Copyright 2014 Sheldon Neilson www.neilson.co.za
 *
 * 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 za.co.neilson.alarm.preferences;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

import za.co.neilson.alarm.Alarm;
import za.co.neilson.alarm.AlarmActivity;
import za.co.neilson.alarm.BaseActivity;
import za.co.neilson.alarm.database.Database;
import za.co.neilson.alarm.group.Group;
import za.co.neilson.alarm.group.User;
import za.co.neilson.alarm.login.ServerRequest;
import za.co.neilson.alarm.preferences.AlarmPreference.Key;
import za.co.neilson.alarm.service.AlarmServiceBroadcastReciever;
import za.co.neilson.alarm.R;
import android.app.AlertDialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Vibrator;
import android.support.v7.app.ActionBar;
import android.util.Log;
import android.view.HapticFeedbackConstants;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckedTextView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

public class AlarmPreferencesActivity extends BaseActivity {

    ImageButton deleteButton;
    TextView okButton;
    TextView cancelButton;
    private Alarm alarm;
    private MediaPlayer mediaPlayer;

    private ListAdapter listAdapter;
    private ListView listView;

    List<NameValuePair> params;
    SharedPreferences pref;

    String email;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // requestWindowFeature(Window.FEATURE_NO_TITLE);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        setContentView(R.layout.alarm_preferences);

        pref = getSharedPreferences("AppPref", MODE_PRIVATE);
        email = getIntent().getStringExtra("email");
        Log.d("? ???!!! ", "" + email);

        Bundle bundle = getIntent().getExtras();
        if (bundle != null && bundle.containsKey("alarm")) {
            setMathAlarm((Alarm) bundle.getSerializable("alarm"));
        } else {
            setMathAlarm(new Alarm());
        }
        if (bundle != null && bundle.containsKey("adapter")) {
            setListAdapter((AlarmPreferenceListAdapter) bundle.getSerializable("adapter"));
        } else {
            setListAdapter(new AlarmPreferenceListAdapter(this, getMathAlarm()));
        }

        getListView().setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> l, View v, int position, long id) {
                final AlarmPreferenceListAdapter alarmPreferenceListAdapter = (AlarmPreferenceListAdapter) getListAdapter();
                final AlarmPreference alarmPreference = (AlarmPreference) alarmPreferenceListAdapter
                        .getItem(position);

                AlertDialog.Builder alert;
                v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
                switch (alarmPreference.getType()) {
                case BOOLEAN:
                    CheckedTextView checkedTextView = (CheckedTextView) v;
                    boolean checked = !checkedTextView.isChecked();
                    ((CheckedTextView) v).setChecked(checked);
                    switch (alarmPreference.getKey()) {
                    case ALARM_ACTIVE:
                        alarm.setAlarmActive(checked);
                        break;
                    case ALARM_VIBRATE:
                        alarm.setVibrate(checked);
                        if (checked) {
                            Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
                            vibrator.vibrate(1000);
                        }
                        break;
                    }
                    alarmPreference.setValue(checked);
                    break;
                case STRING:

                    alert = new AlertDialog.Builder(AlarmPreferencesActivity.this);

                    alert.setTitle(alarmPreference.getTitle());
                    // alert.setMessage(message);

                    // Set an EditText view to get user input
                    final EditText input = new EditText(AlarmPreferencesActivity.this);

                    input.setText(alarmPreference.getValue().toString());

                    alert.setView(input);
                    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {

                            alarmPreference.setValue(input.getText().toString());

                            if (alarmPreference.getKey() == Key.ALARM_NAME) {
                                alarm.setAlarmName(alarmPreference.getValue().toString());
                            }

                            alarmPreferenceListAdapter.setMathAlarm(getMathAlarm());
                            alarmPreferenceListAdapter.notifyDataSetChanged();
                        }
                    });
                    alert.show();
                    break;
                case LIST:
                    alert = new AlertDialog.Builder(AlarmPreferencesActivity.this);

                    alert.setTitle(alarmPreference.getTitle());
                    // alert.setMessage(message);

                    CharSequence[] items = new CharSequence[alarmPreference.getOptions().length];
                    for (int i = 0; i < items.length; i++)
                        items[i] = alarmPreference.getOptions()[i];

                    alert.setItems(items, new OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            switch (alarmPreference.getKey()) {
                            case ALARM_DIFFICULTY:
                                Alarm.Difficulty d = Alarm.Difficulty.values()[which];
                                alarm.setDifficulty(d);
                                break;
                            case ALARM_TONE:
                                alarm.setAlarmTonePath(alarmPreferenceListAdapter.getAlarmTonePaths()[which]);
                                if (alarm.getAlarmTonePath() != null) {
                                    if (mediaPlayer == null) {
                                        mediaPlayer = new MediaPlayer();
                                    } else {
                                        if (mediaPlayer.isPlaying())
                                            mediaPlayer.stop();
                                        mediaPlayer.reset();
                                    }
                                    try {
                                        // mediaPlayer.setVolume(1.0f, 1.0f);
                                        mediaPlayer.setVolume(0.2f, 0.2f);
                                        mediaPlayer.setDataSource(AlarmPreferencesActivity.this,
                                                Uri.parse(alarm.getAlarmTonePath()));
                                        mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
                                        mediaPlayer.setLooping(false);
                                        mediaPlayer.prepare();
                                        mediaPlayer.start();

                                        // Force the mediaPlayer to stop after 3
                                        // seconds...
                                        if (alarmToneTimer != null)
                                            alarmToneTimer.cancel();
                                        alarmToneTimer = new CountDownTimer(3000, 3000) {
                                            @Override
                                            public void onTick(long millisUntilFinished) {

                                            }

                                            @Override
                                            public void onFinish() {
                                                try {
                                                    if (mediaPlayer.isPlaying())
                                                        mediaPlayer.stop();
                                                } catch (Exception e) {

                                                }
                                            }
                                        };
                                        alarmToneTimer.start();
                                    } catch (Exception e) {
                                        try {
                                            if (mediaPlayer.isPlaying())
                                                mediaPlayer.stop();
                                        } catch (Exception e2) {

                                        }
                                    }
                                }
                                break;
                            default:
                                break;
                            }
                            alarmPreferenceListAdapter.setMathAlarm(getMathAlarm());
                            alarmPreferenceListAdapter.notifyDataSetChanged();
                        }

                    });

                    alert.show();
                    break;
                case MULTIPLE_LIST:
                    alert = new AlertDialog.Builder(AlarmPreferencesActivity.this);

                    alert.setTitle(alarmPreference.getTitle());
                    // alert.setMessage(message);

                    CharSequence[] multiListItems = new CharSequence[alarmPreference.getOptions().length];
                    for (int i = 0; i < multiListItems.length; i++)
                        multiListItems[i] = alarmPreference.getOptions()[i];

                    boolean[] checkedItems = new boolean[multiListItems.length];
                    for (Alarm.Day day : getMathAlarm().getDays()) {
                        checkedItems[day.ordinal()] = true;
                    }
                    alert.setMultiChoiceItems(multiListItems, checkedItems, new OnMultiChoiceClickListener() {

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

                            Alarm.Day thisDay = Alarm.Day.values()[which];

                            if (isChecked) {
                                alarm.addDay(thisDay);
                            } else {
                                // Only remove the day if there are more than 1
                                // selected
                                if (alarm.getDays().length > 1) {
                                    alarm.removeDay(thisDay);
                                } else {
                                    // If the last day was unchecked, re-check
                                    // it
                                    ((AlertDialog) dialog).getListView().setItemChecked(which, true);
                                }
                            }

                        }
                    });
                    alert.setOnCancelListener(new OnCancelListener() {
                        @Override
                        public void onCancel(DialogInterface dialog) {
                            alarmPreferenceListAdapter.setMathAlarm(getMathAlarm());
                            alarmPreferenceListAdapter.notifyDataSetChanged();

                        }
                    });
                    alert.show();
                    break;
                case TIME:
                    TimePickerDialog timePickerDialog = new TimePickerDialog(AlarmPreferencesActivity.this,
                            new OnTimeSetListener() {

                                @Override
                                public void onTimeSet(TimePicker timePicker, int hours, int minutes) {
                                    Calendar newAlarmTime = Calendar.getInstance();
                                    newAlarmTime.set(Calendar.HOUR_OF_DAY, hours);
                                    newAlarmTime.set(Calendar.MINUTE, minutes);
                                    newAlarmTime.set(Calendar.SECOND, 0);
                                    alarm.setAlarmTime(newAlarmTime);
                                    alarmPreferenceListAdapter.setMathAlarm(getMathAlarm());
                                    alarmPreferenceListAdapter.notifyDataSetChanged();
                                }
                            }, alarm.getAlarmTime().get(Calendar.HOUR_OF_DAY),
                            alarm.getAlarmTime().get(Calendar.MINUTE), true);
                    timePickerDialog.setTitle(alarmPreference.getTitle());
                    timePickerDialog.show();
                default:
                    break;
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        boolean result = super.onCreateOptionsMenu(menu);
        menu.findItem(R.id.menu_item_new).setVisible(false);
        return result;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_item_save:
            Database.init(getApplicationContext());
            Database.setEmail(email);

            if (getMathAlarm().getId() < 1) {
                getMathAlarm().setEmail(email);
                Database.create(getMathAlarm());
                Bundle bundle = getIntent().getExtras();
                if (bundle != null && bundle.containsKey("user")) {
                    User user = (User) bundle.getSerializable("user");
                    Group group = new Group(getMathAlarm(), user);
                    Database.addGroup(group);
                }
                params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("email", email));
                Log.d("??? ? : ", "" + email);
                params.add(new BasicNameValuePair("time", getMathAlarm().getAlarmTimeString()));
                params.add(new BasicNameValuePair("difficulty",
                        Integer.toString(getMathAlarm().getDifficulty().ordinal())));
                params.add(new BasicNameValuePair("tone", getMathAlarm().getAlarmTonePath()));
                params.add(new BasicNameValuePair("vibrate", Boolean.toString(getMathAlarm().getVibrate())));
                params.add(new BasicNameValuePair("name", getMathAlarm().getAlarmName()));
                //params.add(new BasicNameValuePair("days", Arrays.toString(getMathAlarm().getDays())));
                for (int i = 0; i < getMathAlarm().getDays().length; i++) {
                    params.add(new BasicNameValuePair("days", getMathAlarm().getDays()[i].toString()));
                }
                ServerRequest sr = new ServerRequest();
                JSONObject json = sr.getJSON("http://168.188.123.218:8080/alarmdata", params);
                if (json != null) {
                    try {
                        String jsonstr = json.getString("response");
                        //JSONObject json2 = sr.getJSON("http://168.188.123.218:8080/useralarm", params);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

            } else {
                Database.update(getMathAlarm());
                Database.setEmail(email);

            }

            callMathAlarmScheduleService();
            Toast.makeText(AlarmPreferencesActivity.this, getMathAlarm().getTimeUntilNextAlarmMessage(),
                    Toast.LENGTH_LONG).show();
            finish();
            break;
        case R.id.menu_item_delete:
            AlertDialog.Builder dialog = new AlertDialog.Builder(AlarmPreferencesActivity.this);
            dialog.setTitle("Delete");
            dialog.setMessage("Delete this alarm?");
            dialog.setPositiveButton("Ok", new OnClickListener() {

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

                    Database.init(getApplicationContext());
                    Database.setEmail(email);

                    if (getMathAlarm().getId() < 1) {
                        // Alarm not saved
                    } else {
                        params = new ArrayList<NameValuePair>();
                        params.add(new BasicNameValuePair("email", email));
                        params.add(new BasicNameValuePair("time", alarm.getAlarmTimeString()));
                        Log.d("??? ? : ", "" + email);
                        ServerRequest sr = new ServerRequest();
                        JSONObject json = sr.getJSON("http://168.188.123.218:8080/deletealarm", params);

                        Database.deleteEntry(alarm);
                        callMathAlarmScheduleService();
                    }
                    finish();
                }
            });
            dialog.setNegativeButton("Cancel", new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            dialog.show();

            break;
        }
        return super.onOptionsItemSelected(item);
    }

    private CountDownTimer alarmToneTimer;

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putSerializable("alarm", getMathAlarm());
        outState.putSerializable("adapter", (AlarmPreferenceListAdapter) getListAdapter());
    };

    @Override
    protected void onPause() {
        super.onPause();
        try {
            if (mediaPlayer != null)
                mediaPlayer.release();
        } catch (Exception e) {
        }
        // setListAdapter(null);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    public Alarm getMathAlarm() {
        return alarm;
    }

    public void setMathAlarm(Alarm alarm) {
        this.alarm = alarm;
    }

    public ListAdapter getListAdapter() {
        return listAdapter;
    }

    public void setListAdapter(ListAdapter listAdapter) {
        this.listAdapter = listAdapter;
        getListView().setAdapter(listAdapter);

    }

    public ListView getListView() {
        if (listView == null)
            listView = (ListView) findViewById(android.R.id.list);
        return listView;
    }

    public void setListView(ListView listView) {
        this.listView = listView;
    }

    @Override
    public void onClick(View v) {
        // super.onClick(v);

    }
}