Android Open Source - screenstandby Dock Receiver






From Project

Back to project page screenstandby.

License

The source code is released under:

GNU General Public License

If you think the Android project screenstandby 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) 2014 Hoang Nguyen./* w  w  w  . j  av a 2 s  . co m*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Hoang Nguyen - initial API and implementation
 ******************************************************************************/
package com.nkahoang.screenstandby;

import java.io.DataOutputStream;

import com.nkahoang.kernelswitchobserver.GenericHardwareObserver;
import com.nkahoang.kernelswitchobserver.HardwareNotFoundException;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;

public class DockReceiver extends BroadcastReceiver {
  private static String launchPackage = "";
  private static Boolean killPackage = false;
    public DockReceiver() {
    }
    private static void ScreenOn(Context context)
  {
    Log.w("standby", "DOCK UNPLUGGED");
    Intent serviceIntent = new Intent();
        serviceIntent.setAction(StandbyService.TOGGLE_INTENT);
        context.sendBroadcast(serviceIntent);
        try
        {
          if (killPackage && launchPackage.length() > 0)
          {
            Process proc;
            DataOutputStream os;
              proc = Runtime.getRuntime().exec("su");
                os = new DataOutputStream(proc.getOutputStream());
            os.writeBytes("kill $(pgrep " + launchPackage + ")\n");
          }
        }catch(Exception ex)
        {
          Logger.Log(context, ex.getMessage() + " " + ex.getStackTrace().toString());
        }
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        if (prefs.getBoolean("dockrun_close_returnhome", false))
        {
      Intent startMain = new Intent(Intent.ACTION_MAIN);
      startMain.addCategory(Intent.CATEGORY_HOME);
      startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      context.startActivity(startMain);  
        }
  }
  
  private static void ScreenOff(Context context)
  {
    Log.w("standby", "DOCK PLUGGED");
    Intent serviceIntent = new Intent();
        serviceIntent.setAction(StandbyService.ENABLE_INTENT);
        serviceIntent.putExtra("autohdmi", true);
        context.sendBroadcast(serviceIntent);

        if (launchPackage.length() > 0)
        {
          Intent LaunchIntent = context.getPackageManager().getLaunchIntentForPackage(launchPackage);
          context.startActivity(LaunchIntent);
        }
  }
    
  private static String getDeviceState(String device)
  {
    String status = "N/A";
    try {
      GenericHardwareObserver ob = new GenericHardwareObserver(device);
      status = ob.getCurrentHardwareState();
    } catch (HardwareNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return status;
  }
  @Override
    public void onReceive(Context context, Intent intent) {
    Logger.Log(context, intent);
      SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
      Boolean useDock = prefs.getBoolean("dockdetection", false);
      launchPackage = prefs.getString("dockrunpackage", "");
      killPackage = prefs.getBoolean("dockrun_close", false);
      Boolean screenOff = false;
      
      if (useDock)
      {
        int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, Intent.EXTRA_DOCK_STATE_UNDOCKED);
        //if (state != Intent.EXTRA_DOCK_STATE_UNDOCKED)
        //{
          if (prefs.getBoolean("dockatrix", false))
          {
            if (getDeviceState("whisper_hid").equals("1") && 
              getDeviceState("smartdock").equals("1"))
            {
              String docktype = getDeviceState("extdock");
              if (docktype.equals("3")) //LAPDOCK
              {
                if (prefs.getBoolean("dockatrix_lapdock", true)) {
                  ScreenOff(context);
                  Toast.makeText(context, "Atrix Lapdock detected", Toast.LENGTH_SHORT).show();
                }
              }
              else if (docktype.equals("4")) //HD DOCK
              {
                if (prefs.getBoolean("dockatrix_hddock", true)) {
                  ScreenOff(context);
                  Toast.makeText(context, "Atrix HD dock detected", Toast.LENGTH_SHORT).show();
                }
              }
            }
            else
              ScreenOn(context);
            return;
          }
        //}
        
        switch (state)
        {
          case Intent.EXTRA_DOCK_STATE_CAR:
            screenOff = prefs.getBoolean("dockcar", true);
            break;
          case Intent.EXTRA_DOCK_STATE_DESK:
            screenOff = prefs.getBoolean("dockdesk", true);
            break;
          case Intent.EXTRA_DOCK_STATE_HE_DESK:
            screenOff = prefs.getBoolean("dockanalog", true);
            break;
          case Intent.EXTRA_DOCK_STATE_LE_DESK:
            screenOff = prefs.getBoolean("dockdigital", true);
            break;
          case Intent.EXTRA_DOCK_STATE_UNDOCKED:
            if (prefs.getBoolean("dockremoval", true)) ScreenOn(context);
            return;
        }
        if (screenOff)
          ScreenOff(context);
      }
    }
}




Java Source Code List

android.os.UEventObserver.java
com.nkahoang.kernelswitchobserver.DockObserver.java
com.nkahoang.kernelswitchobserver.GenericHardwareObserver.java
com.nkahoang.kernelswitchobserver.HDMIObserver.java
com.nkahoang.kernelswitchobserver.HardwareNotFoundException.java
com.nkahoang.kernelswitchobserver.HeadsetObserver.java
com.nkahoang.kernelswitchobserver.UEventStateChangeHandler.java
com.nkahoang.screenstandby.AppDetector.java
com.nkahoang.screenstandby.AutoSettingWizard.java
com.nkahoang.screenstandby.BTReceiver.java
com.nkahoang.screenstandby.BackupHelper.java
com.nkahoang.screenstandby.BaseActivity.java
com.nkahoang.screenstandby.BootTimeReceiver.java
com.nkahoang.screenstandby.ChangeLogHandler.java
com.nkahoang.screenstandby.DisableActivity.java
com.nkahoang.screenstandby.DockReceiver.java
com.nkahoang.screenstandby.EnableActivity.java
com.nkahoang.screenstandby.ExceptionHandler.java
com.nkahoang.screenstandby.FlowLayout.java
com.nkahoang.screenstandby.FontManager.java
com.nkahoang.screenstandby.HDMIReceiver.java
com.nkahoang.screenstandby.HUDNative.java
com.nkahoang.screenstandby.HeadsetReceiver.java
com.nkahoang.screenstandby.IconContextMenu.java
com.nkahoang.screenstandby.Logger.java
com.nkahoang.screenstandby.Main.java
com.nkahoang.screenstandby.ManualBrightnessChangerActivity.java
com.nkahoang.screenstandby.MediaButtonControlReceiver.java
com.nkahoang.screenstandby.NotificationBar.java
com.nkahoang.screenstandby.NumberPickerButton.java
com.nkahoang.screenstandby.NumberPicker.java
com.nkahoang.screenstandby.OneTwoDimmer.java
com.nkahoang.screenstandby.OutcomingCallReceiver.java
com.nkahoang.screenstandby.PreferenceListFragment.java
com.nkahoang.screenstandby.RemoteControlSelectActivity.java
com.nkahoang.screenstandby.RemoteControllerActivity.java
com.nkahoang.screenstandby.RemoteServerActivity.java
com.nkahoang.screenstandby.ServiceToggleReceiver.java
com.nkahoang.screenstandby.SettingActivity.java
com.nkahoang.screenstandby.ShortcutActivity.java
com.nkahoang.screenstandby.ShortcutOnActivity.java
com.nkahoang.screenstandby.StandbyService.java
com.nkahoang.screenstandby.StandbyToggleReceiver.java
com.nkahoang.screenstandby.StandbyWidgetProvider2by2.java
com.nkahoang.screenstandby.StandbyWidgetProvider.java
com.nkahoang.screenstandby.TimePickerDialog.java
com.nkahoang.screenstandby.TimePicker.java
com.nkahoang.screenstandby.TroubleshootingActivity.java
com.nkahoang.screenstandby.VolumeKeyDetector.java
com.nkahoang.screenstandby.XTopDPIChanger.java
com.nkahoang.screenstandby.remotecontrol.ClientService.java
com.nkahoang.screenstandby.remotecontrol.Core.java
com.nkahoang.screenstandby.remotecontrol.OnClientStateChangedHandler.java
com.nkahoang.screenstandby.remotecontrol.OnServiceAddressResolvedHandler.java
com.nkahoang.screenstandby.remotecontrol.OnStateChangedHandler.java
com.nkahoang.screenstandby.remotecontrol.RemoteAppPackages.java
com.nkahoang.screenstandby.remotecontrol.RemotePackageInfo.java
com.nkahoang.screenstandby.remotecontrol.ServerService.java
com.nkahoang.screenstandby.remotecontrol.ServiceBinder.java
com.nkahoang.screenstandby.settings.DeviceSettings.java
com.nkahoang.screenstandby.settings.RootChecker.java
com.nkahoang.screenstandby.settings.UpdateChecker.java