Android Open Source - HomeSwipeManager Main 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  w w. ja  va 2s .  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.os.*;
import android.preference.PreferenceManager;
import android.widget.Toast;
import android.app.*;
import android.content.*;

public class MainActivity extends Activity {
  private SharedPreferences preferences;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    preferences = PreferenceManager.getDefaultSharedPreferences(this);
    int action = preferences.getInt("action", Action.ACTION_DO_NOTHING);
    switch(action) { // Executes the user defined action
    case Action.ACTION_LAST_TASK:
      toggleLastTask();
      break;
    case Action.ACTION_OPEN_APP:
      String name = preferences.getString("appName", null);
      String packageName = preferences.getString("appPackage", null);
      if(name!=null && packageName!=null) {
        try {
          Intent intent = new Intent("android.intent.action.MAIN");
            intent.setComponent(ComponentName.unflattenFromString(packageName+"/"+name));
            intent.addCategory("android.intent.category.LAUNCHER");
            startActivity(intent);
        } catch(Exception e) {
          Toast.makeText(this, R.string.errorNoApp, Toast.LENGTH_LONG).show();
        }
      } else {
        Toast.makeText(this, R.string.errorNoApp, Toast.LENGTH_LONG).show();
      }
      break;
    case Action.ACTION_DO_NOTHING:
    default:
      break;
    }
    finish();
  }
  
  /**
   * Moves the last task to the foreground, excluding home screen launchers
   */
  private void toggleLastTask() {       
        final ActivityManager am = (ActivityManager)getSystemService(Activity.ACTIVITY_SERVICE);
        List <ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(5);
        String packageName;
        for(int i=2; i<tasks.size(); i++) {
            packageName = tasks.get(i).topActivity.getPackageName();
            if(!packageName.startsWith("com.android.systemui") && !isLauncher(packageName)) {
        am.moveTaskToFront(tasks.get(i).id, 0);
        return;
      }
        }
    }
  
  /**
   * Checks if the package is a home screen launcher
   * @param packageName The package to be checked
   * @return true if the packages is a launcher, false otherwise
   */
  private boolean isLauncher(String packageName) {
    String[] launchers = HomeSwipeManagerApp.getLaunchers();
    for(String launcher : launchers) {
      if(launcher.equals(packageName)) return true;
    }
    return false;
  }

}




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