Android Open Source - HomeSwipeManager Settings Activity






From Project

Back to project page HomeSwipeManager.

License

The source code is released under:

Apache License

If you think the Android project HomeSwipeManager 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 2013 Andrea De Cesare
*//from w  ww . ja  va2 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 com.andreadec.homeswipemanager;

import java.util.*;
import android.app.*;
import android.content.*;
import android.os.*;
import android.preference.*;
import android.view.*;
import android.widget.*;
import android.widget.AdapterView.*;

public class SettingsActivity extends Activity implements OnItemSelectedListener {
  private SharedPreferences preferences;
  private ArrayAdapter<Action> actionsAdapter;
  private AppsArrayAdapter appsAdapter;
  private Spinner spinnerAction, spinnerAppChooser;
  private TextView textViewChooseApp;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);
    preferences = PreferenceManager.getDefaultSharedPreferences(this);
    
    spinnerAction = (Spinner)findViewById(R.id.spinnerAction);
    spinnerAction.setOnItemSelectedListener(this);
    
    textViewChooseApp = (TextView)findViewById(R.id.textViewChooseApp);
    spinnerAppChooser = (Spinner)findViewById(R.id.spinnerAppChooser);
    spinnerAppChooser.setOnItemSelectedListener(this);
    
    ArrayList<Action> actions = Action.getAvailableActions(this);
    actionsAdapter = new ArrayAdapter<Action>(this, android.R.layout.simple_spinner_item, actions);
    actionsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerAction.setAdapter(actionsAdapter);
    
    int savedAction = preferences.getInt("action", Action.ACTION_DO_NOTHING);
    spinnerAction.setSelection(savedAction);
  }

  @Override
  public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    SharedPreferences.Editor editor = preferences.edit();
    if(parent.equals(spinnerAction)) {
      Action action = actionsAdapter.getItem(position);
      editor.putInt("action", action.action);
      
      if(action.action==Action.ACTION_OPEN_APP) {
        showAppChooser();
      } else {
        hideAppChooser();
      }
    } else if(parent.equals(spinnerAppChooser)) {
      App app = appsAdapter.getItem(position);
      editor.putString("appName", app.name);
      editor.putString("appPackage", app.packageName);
    }
    editor.commit();
  }
  
  private void showAppChooser() {
    new LoadAppsTask().execute();
  }
  private void hideAppChooser() {
    textViewChooseApp.setVisibility(View.GONE);
    spinnerAppChooser.setVisibility(View.GONE);
  }
  
  private int getAppPosition(String name) {
    if(name==null) return -1;
    ArrayList<App> apps = appsAdapter.getItems();
    for(int i=0; i<apps.size(); i++) {
      if(apps.get(i).name.equals(name)) {
        return i;
      }
    }
    return -1;
  }
  
  private class LoadAppsTask extends AsyncTask<Void, Void, Void> {
    private ProgressDialog progressDialog;
    int appPosition;
    
    public LoadAppsTask() {
      progressDialog = new ProgressDialog(SettingsActivity.this);
      progressDialog.setCancelable(false);
      progressDialog.setMessage(SettingsActivity.this.getResources().getString(R.string.loadingApps));
    }
    
    @Override
    protected void onPreExecute() {
      progressDialog.show();
    }
    
    @Override
    protected Void doInBackground(Void... params) {
      appsAdapter = new AppsArrayAdapter(SettingsActivity.this, App.getApps(SettingsActivity.this));
      appPosition = getAppPosition(preferences.getString("appName", null));
      return null;
    }
    
    @Override
    protected void onPostExecute(final Void success) {
      spinnerAppChooser.setAdapter(appsAdapter);
      if(appPosition!=-1) {
        spinnerAppChooser.setSelection(appPosition);
      }
      textViewChooseApp.setVisibility(View.VISIBLE);
      spinnerAppChooser.setVisibility(View.VISIBLE);
      progressDialog.dismiss();
    }
  }

  @Override
  public void onNothingSelected(AdapterView<?> parent) {}
}




Java Source Code List

com.andreadec.homeswipemanager.Action.java
com.andreadec.homeswipemanager.App.java
com.andreadec.homeswipemanager.AppsArrayAdapter.java
com.andreadec.homeswipemanager.HomeSwipeManagerApp.java
com.andreadec.homeswipemanager.MainActivity.java
com.andreadec.homeswipemanager.SettingsActivity.java