Android Open Source - hpush Setting Activity






From Project

Back to project page hpush.

License

The source code is released under:

MIT License

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

package com.hpush.app.activities;
/*from w  w w.jav  a  2  s.  c  om*/
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.TypedArray;
import android.os.Build;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.support.v4.os.AsyncTaskCompat;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.MarginLayoutParams;

import com.chopping.application.LL;
import com.chopping.bus.ApplicationConfigurationDownloadedEvent;
import com.chopping.bus.ApplicationConfigurationLoadingIgnoredEvent;
import com.chopping.exceptions.CanNotOpenOrFindAppPropertiesException;
import com.chopping.exceptions.InvalidAppPropertiesException;
import com.hpush.R;
import com.hpush.bus.DeleteAccountEvent;
import com.hpush.bus.EditSettingsEvent;
import com.hpush.bus.InsertAccountEvent;
import com.hpush.gcm.RegGCMTask;
import com.hpush.gcm.UnregGCMTask;
import com.hpush.utils.Prefs;

import de.greenrobot.event.EventBus;


/**
 * Setting .
 */
public final class SettingActivity extends PreferenceActivity implements Preference.OnPreferenceChangeListener {

  /**
   * The "ActionBar".
   */
  private Toolbar mToolbar;

  /**
   * Progress indicator.
   */
  private ProgressDialog mPb;
  /**
   * Flag for any change of push.
   */
  private boolean mChangedPushStatus;
  //------------------------------------------------
  //Subscribes, event-handlers
  //------------------------------------------------

  /**
   * Handler for {@link com.chopping.bus.ApplicationConfigurationDownloadedEvent}
   *
   * @param e
   *     Event {@link  com.chopping.bus.ApplicationConfigurationDownloadedEvent}.
   */
  public void onEvent(ApplicationConfigurationDownloadedEvent e) {
    onAppConfigLoaded();
  }

  /**
   * Handler for {@link com.chopping.bus.ApplicationConfigurationLoadingIgnoredEvent}.
   *
   * @param e
   *     Event {@link com.chopping.bus.ApplicationConfigurationLoadingIgnoredEvent}.
   */
  public void onEvent(ApplicationConfigurationLoadingIgnoredEvent e) {
    LL.i("Ignored a change to load application's configuration.");
    onAppConfigIgnored();
  }

  //------------------------------------------------

  /**
   * Show an instance of SettingsActivity.
   *
   * @param context
   *     A context object.
   *  @param openSettingV
   *     The view that open the {@link com.hpush.app.activities.WebViewActivity}.
   */
  public static void showInstance(Activity context, View openSettingV) {
    Intent intent = new Intent(context, SettingActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
//    if (openSettingV != null && (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) ){
//      ActivityOptionsCompat transitionActivityOptions = ActivityOptionsCompat.makeSceneTransitionAnimation(context,
//          Pair.create(openSettingV, "openSettingV"));
//      context.startActivity(intent, transitionActivityOptions.toBundle());
//    } else {
      context.startActivity(intent);
//    }
  }


  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Prefs prefs = Prefs.getInstance(getApplication());
    if(getResources().getBoolean(R.bool.landscape)) {
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }
    addPreferencesFromResource(R.xml.settings);
    mPb = ProgressDialog.show(this, null, getString(R.string.msg_app_init));
    mPb.setCancelable(true);
    mToolbar = (Toolbar) getLayoutInflater().inflate(R.layout.toolbar, null, false);
    addContentView(mToolbar, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    mToolbar.setTitle(R.string.application_name);
    mToolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
    mToolbar.setNavigationOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        backPressed();
      }
    });

    CheckBoxPreference push = (CheckBoxPreference) findPreference(Prefs.KEY_PUSH_SETTING);
    push.setSummary( getString(prefs.isPushTurnedOn() ? R.string.setting_push_on :R.string.setting_push_off ));
    push.setOnPreferenceChangeListener(this);

    CheckBoxPreference fullText = (CheckBoxPreference) findPreference(Prefs.KEY_FULL_TEXT);
    fullText.setOnPreferenceChangeListener(this);

    CheckBoxPreference allowEmptyUrl = (CheckBoxPreference) findPreference(Prefs.KEY_ALLOW_EMPTY_URL);
    allowEmptyUrl.setOnPreferenceChangeListener(this);

    EditTextPreference count = (EditTextPreference) findPreference(Prefs.KEY_MSG_COUNT);
    count.setSummary(getString(R.string.setting_messages_count, prefs.getMsgCount()));
    count.setText(prefs.getMsgCount());
    count.setOnPreferenceChangeListener(this);

    ListPreference sort = (ListPreference) findPreference(Prefs.KEY_SORT_TYPE);
    String value = prefs.getSortTypeValue();
    sort.setValue(value);
    int pos = Integer.valueOf(value);
    String[] arr = getResources().getStringArray(R.array.setting_sort_types);
    sort.setSummary(arr[pos]);
    sort.setOnPreferenceChangeListener(this);



    ListPreference sound = (ListPreference) findPreference(Prefs.KEY_SOUND_TYPE);
    String soundValue = prefs.getSoundTypeValue();
    sound.setValue(soundValue);
    int posSound = Integer.valueOf(soundValue);
    String[] arrSound = getResources().getStringArray(R.array.setting_sound_types);
    sound.setSummary(arrSound[posSound]);
    sound.setOnPreferenceChangeListener(this);

    ((MarginLayoutParams) findViewById(android.R.id.list).getLayoutParams()).topMargin = getActionBarHeight(this);
  }


  /**
   * Get height of {@link android.support.v7.app.ActionBar}.
   *
   * @param activity
   *     {@link android.app.Activity} that hosts an  {@link android.support.v7.app.ActionBar}.
   *
   * @return Height of bar.
   */
  public static int getActionBarHeight(Activity activity) {
    int[] abSzAttr;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
      abSzAttr = new int[] { android.R.attr.actionBarSize };
    } else {
      abSzAttr = new int[] { R.attr.actionBarSize };
    }
    TypedArray a = activity.obtainStyledAttributes(abSzAttr);
    return a.getDimensionPixelSize(0, -1);
  }


  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
      backPressed();
      break;
    }
    return super.onOptionsItemSelected(item);
  }


  @Override
  public boolean onPreferenceChange(Preference preference, Object newValue) {
    if (preference.getKey().equals(Prefs.KEY_PUSH_SETTING)) {
      mChangedPushStatus = true;
      EventBus.getDefault().removeStickyEvent(InsertAccountEvent.class);
      EventBus.getDefault().removeStickyEvent(DeleteAccountEvent.class);
      if (!Boolean.valueOf(newValue.toString())) {
        AsyncTaskCompat.executeParallel(new UnregGCMTask(getApplication() ) {
          ProgressDialog dlg;

          @Override
          protected void onPreExecute() {
            super.onPreExecute();
            dlg = ProgressDialog.show(SettingActivity.this, null, getString(
                R.string.msg_push_unregistering));
            dlg.setCancelable(false);
          }

          @Override
          protected void onPostExecute(String regId) {
            super.onPostExecute(regId);
            dlg.dismiss();
          }
        });
      } else {
        AsyncTaskCompat.executeParallel(new RegGCMTask(getApplication()) {
          ProgressDialog dlg;

          @Override
          protected void onPreExecute() {
            super.onPreExecute();
            dlg = ProgressDialog.show(SettingActivity.this, null, getString(R.string.msg_push_registering));
            dlg.setCancelable(false);
          }

          @Override
          protected void onPostExecute(String regId) {
            super.onPostExecute(regId);
            if (!TextUtils.isEmpty(regId)) {
              dlg.dismiss();
            }
          }
        });
      }
    }

    if (preference.getKey().equals(Prefs.KEY_MSG_COUNT)) {
      int count = Integer.valueOf(newValue.toString());
      int max = getResources().getInteger(R.integer.max_msg_count);
      if (count > max) {
        count = max;
        Prefs.getInstance(getApplication()).setMsgCount(max+"");
        preference.setSummary(getString(R.string.setting_messages_count, count + ""));
        return false;
      }
      preference.setSummary(getString(R.string.setting_messages_count, count + ""));
    }

    if(preference.getKey().equals(Prefs.KEY_SORT_TYPE)) {
      int pos = Integer.valueOf(newValue.toString());
      String[] arr = getResources().getStringArray(R.array.setting_sort_types);
      preference.setSummary(arr[pos]);
    }


    if(preference.getKey().equals(Prefs.KEY_SOUND_TYPE)) {
      int pos = Integer.valueOf(newValue.toString());
      String[] arr = getResources().getStringArray(R.array.setting_sound_types);
      preference.setSummary(arr[pos]);
    }
    return true;
  }


  @Override
  protected void onResume() {
    EventBus.getDefault().registerSticky(this);
    super.onResume();

    String mightError = null;
    try {
      Prefs.getInstance(getApplication()).downloadApplicationConfiguration();
    } catch (InvalidAppPropertiesException _e) {
      mightError = _e.getMessage();
    } catch (CanNotOpenOrFindAppPropertiesException _e) {
      mightError = _e.getMessage();
    }
    if (mightError != null) {
      new AlertDialog.Builder(this).setTitle(com.chopping.R.string.app_name).setMessage(mightError).setCancelable(
          false).setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
          finish();
        }
      }).create().show();
    }
  }

  @Override
  protected void onPause() {
    EventBus.getDefault().unregister(this);
    super.onPause();
  }

  /**
   * Remove the progress indicator.
   */
  private void dismissPb() {
    if (mPb != null && mPb.isShowing()) {
      mPb.dismiss();
    }
  }

  @Override
  public void onBackPressed() {
    backPressed();
  }

  private void onAppConfigLoaded() {
    dismissPb();
  }

  private void onAppConfigIgnored() {
    dismissPb();
  }

  private void backPressed() {
    if(!mChangedPushStatus) {
      EventBus.getDefault().postSticky(new EditSettingsEvent());
    }
    dismissPb();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      finishAfterTransition();
    } else {
      finish();
    }
  }
}




Java Source Code List

com.hpush.app.App.java
com.hpush.app.activities.BasicActivity.java
com.hpush.app.activities.DailiesActivity.java
com.hpush.app.activities.MainActivity.java
com.hpush.app.activities.SettingActivity.java
com.hpush.app.activities.WebViewActivity.java
com.hpush.app.adapters.DailiesListAdapter.java
com.hpush.app.adapters.MainViewPagerAdapter.java
com.hpush.app.adapters.MessagesListAdapter.java
com.hpush.app.fragments.AboutDialogFragment.java
com.hpush.app.fragments.AdFragment.java
com.hpush.app.fragments.AppListImpFragment.java
com.hpush.app.fragments.BookmarksListFragment.java
com.hpush.app.fragments.DailiesLstFragment.java
com.hpush.app.fragments.GPlusFragment.java
com.hpush.app.fragments.MessagesListFragment.java
com.hpush.bus.BookmarkAllEvent.java
com.hpush.bus.BookmarkMessageEvent.java
com.hpush.bus.BookmarkedEvent.java
com.hpush.bus.ClickMessageCommentsEvent.java
com.hpush.bus.ClickMessageEvent.java
com.hpush.bus.ClickMessageLinkEvent.java
com.hpush.bus.DeleteAccountEvent.java
com.hpush.bus.EULAConfirmedEvent.java
com.hpush.bus.EULARejectEvent.java
com.hpush.bus.EditSettingsEvent.java
com.hpush.bus.GCMRegistedEvent.java
com.hpush.bus.InsertAccountEvent.java
com.hpush.bus.LoadAllEvent.java
com.hpush.bus.LoginedGPlusEvent.java
com.hpush.bus.LogoutGPlusEvent.java
com.hpush.bus.RemoveAllEvent.java
com.hpush.bus.SelectMessageEvent.java
com.hpush.bus.ShareMessageEvent.java
com.hpush.bus.ShowActionBar.java
com.hpush.bus.SortAllEvent.java
com.hpush.bus.UpdateCurrentTotalMessagesEvent.java
com.hpush.data.DailyListItem.java
com.hpush.data.Daily.java
com.hpush.data.FunctionType.java
com.hpush.data.MessageListItem.java
com.hpush.data.Message.java
com.hpush.data.Status.java
com.hpush.data.SyncList.java
com.hpush.db.BookmarksTbl.java
com.hpush.db.DB.java
com.hpush.db.DailyTbl.java
com.hpush.db.DatabaseHelper.java
com.hpush.db.MessagesTbl.java
com.hpush.gcm.ChangeSettingsTask.java
com.hpush.gcm.GcmBroadcastReceiver.java
com.hpush.gcm.GcmIntentService.java
com.hpush.gcm.RegGCMTask.java
com.hpush.gcm.SyncTask.java
com.hpush.gcm.UnregGCMTask.java
com.hpush.utils.Prefs.java
com.hpush.utils.Utils.java
com.hpush.views.FontTextView.java
com.hpush.views.OnViewAnimatedClickedListener2.java
com.hpush.views.OnViewAnimatedClickedListener3.java
com.hpush.views.OnViewAnimatedClickedListener.java
com.hpush.views.SortActionViewProvider.java
com.hpush.views.WebViewEx.java