Pavle_nis

OTGService

Feb 5th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.06 KB | None | 0 0
  1. public class OtgService extends Service
  2. {
  3.  
  4.     private boolean mOtgConnected = false;
  5.     private Handler mHandler;
  6.  
  7.     Timer taskTimer = new Timer();
  8.  
  9.     TimerTask task = new TimerTask()
  10.     {
  11.         private int last_Length = -1;
  12.  
  13.         @Override
  14.         public void run()
  15.         {
  16.             Context context = OtgService.this.getBaseContext();
  17.  
  18.             File directory = new File("/sys/bus/usb/devices");
  19.             File[] contents = directory.listFiles();
  20.  
  21.             int conn_length = contents.length;
  22.  
  23.             if(conn_length ==last_Length)
  24.             {
  25.                 return;
  26.             }
  27.             else
  28.             {
  29.                 last_Length = conn_length;
  30.             }
  31.  
  32.             if(conn_length == 0)
  33.             {
  34.                 mOtgConnected = false;
  35.                 mHandler.post(flagChangedTask);
  36.             }
  37.             else if (conn_length > 0)  //Might get a -1
  38.             {
  39.                 mOtgConnected = true;
  40.                 mHandler.post(flagChangedTask);
  41.  
  42.             }
  43.  
  44.             if(conn_length == 0)
  45.             {
  46.                 displayDialog(false);
  47.             }
  48.             else if (conn_length > 0)  //Might get a -1
  49.             {
  50.                 displayDialog(true);
  51.             }
  52.  
  53.         }
  54.     };
  55.  
  56.     //Will post this to the main thread
  57.     Runnable flagChangedTask = new Runnable()
  58.     {
  59.         @Override
  60.         public void run()
  61.         {
  62.             if (mOtgConnected)
  63.                 Toast.makeText(OtgService.this,"otg connected",Toast.LENGTH_SHORT).show();
  64.             else
  65.                 Toast.makeText(OtgService.this,"otg not connected",Toast.LENGTH_SHORT).show();
  66.         }
  67.     };
  68.  
  69.  
  70.     public OtgService()
  71.     {
  72.  
  73.     }
  74.  
  75.     public void displayDialog(boolean isConnected)
  76.     {
  77.         Intent intent = new Intent(this, MainActivity.class);
  78.         intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
  79.         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  80.  
  81.         intent.putExtra(MainActivity.IS_CONNECTED_KEY, isConnected);
  82.  
  83.         startActivity(intent);
  84.     }
  85.  
  86.     private void onStartCompat(Intent intent)
  87.     {
  88.         Log.e("###", "Starting service!");
  89.  
  90.         if (mHandler == null)
  91.             mHandler = new Handler(getMainLooper());
  92.  
  93.         taskTimer.scheduleAtFixedRate(task, 0, 1000);
  94.     }
  95.  
  96.     // This is the old onStart method that will be called on the pre-2.0
  97.     // platform.  On 2.0 or later we override onStartCommand() so this
  98.     // method will not be called.
  99.     @Override
  100.     public void onStart(Intent intent, int startId)
  101.     {
  102.         onStartCompat(intent);
  103.     }
  104.  
  105.     @Override
  106.     public int onStartCommand(Intent intent, int flags, int startId)
  107.     {
  108.         onStartCompat(intent);
  109.         // We want this service to continue running until it is explicitly
  110.         // stopped, so return sticky.
  111.         return START_STICKY;
  112.     }
  113.  
  114.     @Override
  115.     public void onDestroy()
  116.     {
  117.         //task.cancel();
  118.     }
  119.  
  120.  
  121.     @Override
  122.     public IBinder onBind(Intent intent)
  123.     {
  124.         return null;
  125.     }
  126. }
Add Comment
Please, Sign In to add comment