IBeaconService.java 18.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
package com.radiusnetworks.ibeacon.service;

import android.annotation.TargetApi;
import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothAdapter.LeScanCallback;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.os.AsyncTask;
import android.os.Binder;
import android.os.Build.VERSION;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.util.Log;
import com.radiusnetworks.bluetooth.BluetoothCrashResolver;
import com.radiusnetworks.ibeacon.IBeacon;
import com.radiusnetworks.ibeacon.IBeaconManager;
import com.radiusnetworks.ibeacon.Region;
import com.radiusnetworks.ibeacon.simulator.BeaconSimulator;
import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

@TargetApi(5)
public class IBeaconService
  extends Service
{
  public static final int MSG_SET_SCAN_PERIODS = 6;
  public static final int MSG_START_MONITORING = 4;
  public static final int MSG_START_RANGING = 2;
  public static final int MSG_STOP_MONITORING = 5;
  public static final int MSG_STOP_RANGING = 3;
  public static final String TAG = "IBeaconService";
  int a;
  final Messenger b = new Messenger(new a(this));
  private Map<Region, RangeState> c = new HashMap();
  private Map<Region, MonitorState> d = new HashMap();
  private BluetoothAdapter e;
  private boolean f;
  private boolean g;
  private Date h = new Date();
  private HashSet<IBeacon> i;
  private Handler j = new Handler();
  private int k = 0;
  private BluetoothCrashResolver l;
  private boolean m = false;
  private boolean n = false;
  private long o = 1100L;
  private long p = 0L;
  private List<IBeacon> q = null;
  private int r = 1;
  private long s = 0L;
  private long t = 0L;
  private long u = 0L;
  private long v = 0L;
  private Object w;
  
  private static List<Region> a(IBeacon paramIBeacon, Collection<Region> paramCollection)
  {
    ArrayList localArrayList = new ArrayList();
    paramCollection = paramCollection.iterator();
    while (paramCollection.hasNext())
    {
      Region localRegion = (Region)paramCollection.next();
      if (localRegion.matchesIBeacon(paramIBeacon)) {
        localArrayList.add(localRegion);
      } else if (IBeaconManager.debug) {
        new StringBuilder("This region does not match: ").append(localRegion);
      }
    }
    return localArrayList;
  }
  
  private void a()
  {
    long l1 = 1000L;
    long l2 = this.v - new Date().getTime();
    if (l2 > 0L)
    {
      if (IBeaconManager.debug) {
        new StringBuilder("Waiting to stop scan for another ").append(l2).append(" milliseconds");
      }
      Handler localHandler = this.j;
      Runnable local2 = new Runnable()
      {
        public final void run()
        {
          IBeaconService.a(IBeaconService.this);
        }
      };
      if (l2 > 1000L) {}
      for (;;)
      {
        localHandler.postDelayed(local2, l1);
        return;
        l1 = l2;
      }
    }
    b();
  }
  
  private void a(IBeacon paramIBeacon)
  {
    this.h = new Date();
    this.a += 1;
    if ((this.i.contains(paramIBeacon)) && (IBeaconManager.debug)) {
      new StringBuilder("iBeacon detected multiple times in scan cycle :").append(paramIBeacon.getProximityUuid()).append(" ").append(paramIBeacon.getMajor()).append(" ").append(paramIBeacon.getMinor());
    }
    this.i.add(paramIBeacon);
    if (IBeaconManager.debug) {
      new StringBuilder("iBeacon detected :").append(paramIBeacon.getProximityUuid()).append(" ").append(paramIBeacon.getMajor()).append(" ").append(paramIBeacon.getMinor());
    }
    synchronized (this.d)
    {
      ??? = a(paramIBeacon, this.d.keySet());
      ??? = ((List)???).iterator();
      while (((Iterator)???).hasNext())
      {
        ??? = (Region)((Iterator)???).next();
        MonitorState localMonitorState = (MonitorState)this.d.get(???);
        if (localMonitorState.markInside()) {
          localMonitorState.getCallback().call(this, "monitoringData", new MonitoringData(localMonitorState.isInside(), (Region)???));
        }
      }
    }
    synchronized (this.c)
    {
      ??? = a(paramIBeacon, this.c.keySet());
      ??? = ((List)???).iterator();
      while (((Iterator)???).hasNext())
      {
        ??? = (Region)((Iterator)???).next();
        if (IBeaconManager.debug) {
          new StringBuilder("matches ranging region: ").append(???);
        }
        synchronized ((RangeState)this.c.get(???))
        {
          ((RangeState)???).addIBeacon(paramIBeacon);
        }
      }
    }
  }
  
  @TargetApi(18)
  private void a(Boolean paramBoolean)
  {
    long l1 = 1000L;
    this.m = true;
    if (Build.VERSION.SDK_INT < 18) {
      Log.w("IBeaconService", "Not supported prior to API 18.");
    }
    do
    {
      return;
      if (e() == null)
      {
        Log.e("IBeaconService", "No bluetooth adapter.  iBeaconService cannot scan.");
        if ((this.q == null) && (IBeaconManager.getBeaconSimulator() == null))
        {
          Log.w("IBeaconService", "exiting");
          return;
        }
        Log.w("IBeaconService", "proceeding with simulated scan data");
      }
      if (paramBoolean.booleanValue())
      {
        long l2 = this.u - new Date().getTime();
        if (l2 > 0L)
        {
          if (IBeaconManager.debug) {
            new StringBuilder("Waiting to start next bluetooth scan for another ").append(l2).append(" milliseconds");
          }
          paramBoolean = this.j;
          Runnable local1 = new Runnable()
          {
            public final void run()
            {
              IBeaconService.a(IBeaconService.this, Boolean.valueOf(true));
            }
          };
          if (l2 > 1000L) {}
          for (;;)
          {
            paramBoolean.postDelayed(local1, l1);
            return;
            l1 = l2;
          }
        }
        this.i = new HashSet();
        this.a = 0;
        if ((!this.f) || (this.g == true))
        {
          this.f = true;
          this.g = false;
        }
        for (;;)
        {
          try
          {
            if (e() != null)
            {
              if (!e().isEnabled()) {
                continue;
              }
              if (!this.l.isRecoveryInProgress()) {
                continue;
              }
              Log.w("IBeaconService", "Skipping scan because crash recovery is in progress.");
              this.s = new Date().getTime();
            }
          }
          catch (Exception paramBoolean)
          {
            Log.e("TAG", "Exception starting bluetooth scan.  Perhaps bluetooth is disabled or unavailable?");
            continue;
            Log.w("IBeaconService", "Bluetooth is disabled.  Cannot scan for iBeacons.");
            continue;
          }
          this.v = (new Date().getTime() + this.o);
          a();
          return;
          if (this.n) {
            e().startLeScan((BluetoothAdapter.LeScanCallback)c());
          }
        }
      }
      this.f = false;
    } while (e() == null);
    e().stopLeScan((BluetoothAdapter.LeScanCallback)c());
    this.t = new Date().getTime();
  }
  
  @TargetApi(18)
  private void b()
  {
    if (Build.VERSION.SDK_INT < 18) {
      Log.w("IBeaconService", "Not supported prior to API 18.");
    }
    Object localObject;
    do
    {
      return;
      localObject = this.d.keySet().iterator();
      while (((Iterator)localObject).hasNext())
      {
        Region localRegion = (Region)((Iterator)localObject).next();
        MonitorState localMonitorState = (MonitorState)this.d.get(localRegion);
        if (localMonitorState.isNewlyOutside())
        {
          if (IBeaconManager.debug) {
            new StringBuilder("found a monitor that expired: ").append(localRegion);
          }
          localMonitorState.getCallback().call(this, "monitoringData", new MonitoringData(localMonitorState.isInside(), localRegion));
        }
      }
    } while (this.f != true);
    d();
    if (this.q != null)
    {
      Log.w("IBeaconService", "Simulated scan data is deprecated and will be removed in a future release. Please use the new BeaconSimulator interface instead.");
      localObject = getApplicationInfo();
      i1 = ((ApplicationInfo)localObject).flags & 0x2;
      ((ApplicationInfo)localObject).flags = i1;
      if (i1 != 0)
      {
        localObject = this.q.iterator();
        while (((Iterator)localObject).hasNext()) {
          a((IBeacon)((Iterator)localObject).next());
        }
      }
      Log.w("IBeaconService", "Simulated scan data provided, but ignored because we are not running in debug mode.  Please remove simulated scan data for production.");
    }
    if (IBeaconManager.getBeaconSimulator() != null)
    {
      if (IBeaconManager.getBeaconSimulator().getBeacons() != null)
      {
        localObject = getApplicationInfo();
        i1 = ((ApplicationInfo)localObject).flags & 0x2;
        ((ApplicationInfo)localObject).flags = i1;
        if (i1 != 0)
        {
          localObject = IBeaconManager.getBeaconSimulator().getBeacons().iterator();
          while (((Iterator)localObject).hasNext()) {
            a((IBeacon)((Iterator)localObject).next());
          }
        }
        Log.w("IBeaconService", "Beacon simulations provided, but ignored because we are not running in debug mode.  Please remove beacon simulations for production.");
      }
    }
    else
    {
      if (e() != null)
      {
        if (!e().isEnabled()) {
          break label469;
        }
        e().stopLeScan((BluetoothAdapter.LeScanCallback)c());
        this.t = new Date().getTime();
      }
      label349:
      if (this.c.size() + this.d.size() <= 0) {
        break label481;
      }
    }
    label469:
    label481:
    for (int i1 = 1;; i1 = 0)
    {
      if (i1 == 0) {
        break label486;
      }
      if (IBeaconManager.debug) {
        new StringBuilder("Restarting scan.  Unique beacons seen last cycle: ").append(this.i.size()).append(" Total iBeacon advertisement packets seen: ").append(this.a);
      }
      this.g = true;
      this.u = (new Date().getTime() + this.p);
      if (!this.n) {
        break label486;
      }
      a(Boolean.valueOf(true));
      return;
      Log.w("IBeaconService", "getBeacons is returning null. No simulated beacons to report.");
      break;
      Log.w("IBeaconService", "Bluetooth is disabled.  Cannot scan for iBeacons.");
      break label349;
    }
    label486:
    this.m = false;
  }
  
  @TargetApi(18)
  private Object c()
  {
    if (this.w == null) {
      this.w = new BluetoothAdapter.LeScanCallback()
      {
        public final void onLeScan(BluetoothDevice paramAnonymousBluetoothDevice, int paramAnonymousInt, byte[] paramAnonymousArrayOfByte)
        {
          new IBeaconService.c(IBeaconService.this, (byte)0).execute(new IBeaconService.b[] { new IBeaconService.b(IBeaconService.this, paramAnonymousBluetoothDevice, paramAnonymousInt, paramAnonymousArrayOfByte) });
        }
      };
    }
    return this.w;
  }
  
  private void d()
  {
    Iterator localIterator = this.c.keySet().iterator();
    while (localIterator.hasNext())
    {
      Region localRegion = (Region)localIterator.next();
      RangeState localRangeState = (RangeState)this.c.get(localRegion);
      localRangeState.getCallback().call(this, "rangingData", new RangingData(localRangeState.finalizeIBeacons(), localRegion));
    }
  }
  
  @TargetApi(18)
  private BluetoothAdapter e()
  {
    if (Build.VERSION.SDK_INT < 18)
    {
      Log.w("IBeaconService", "Not supported prior to API 18.");
      return null;
    }
    if (this.e == null) {
      this.e = ((BluetoothManager)getApplicationContext().getSystemService("bluetooth")).getAdapter();
    }
    return this.e;
  }
  
  public void disableScanning()
  {
    this.n = false;
  }
  
  public void enableScanning()
  {
    this.n = true;
    if (!this.m) {
      a(Boolean.valueOf(true));
    }
  }
  
  public IBinder onBind(Intent paramIntent)
  {
    Log.i("IBeaconService", "binding");
    this.k += 1;
    return this.b.getBinder();
  }
  
  public void onCreate()
  {
    e();
    this.l = new BluetoothCrashResolver(this);
    this.l.start();
    try
    {
      this.q = ((List)Class.forName("com.radiusnetworks.ibeacon.SimulatedScanData").getField("iBeacons").get(null));
      return;
    }
    catch (Exception localException)
    {
      Log.e("IBeaconService", "Cannot get simulated Scan data.  Make sure your com.radiusnetworks.ibeacon.SimulatedScanData class defines a field with the signature 'public static List<IBeacon> iBeacons'", localException);
      return;
    }
    catch (ClassNotFoundException localClassNotFoundException) {}
  }
  
  @TargetApi(18)
  public void onDestroy()
  {
    if (Build.VERSION.SDK_INT < 18) {
      Log.w("IBeaconService", "Not supported prior to API 18.");
    }
    do
    {
      return;
      this.l.stop();
      Log.i("IBeaconService", "onDestroy called.  stopping scanning");
      this.j.removeCallbacksAndMessages(null);
      a(Boolean.valueOf(false));
    } while (this.e == null);
    this.e.stopLeScan((BluetoothAdapter.LeScanCallback)c());
    this.t = new Date().getTime();
  }
  
  public boolean onUnbind(Intent paramIntent)
  {
    Log.i("IBeaconService", "unbinding");
    this.k -= 1;
    return false;
  }
  
  public void setScanPeriods(long paramLong1, long paramLong2)
  {
    this.o = paramLong1;
    this.p = paramLong2;
    long l1 = new Date().getTime();
    if (this.u > l1)
    {
      paramLong2 = this.t + paramLong2;
      if (paramLong2 < this.u)
      {
        this.u = paramLong2;
        Log.i("IBeaconService", "Adjusted nextScanStartTime to be " + new Date(this.u));
      }
    }
    if (this.v > l1)
    {
      paramLong1 = this.s + paramLong1;
      if (paramLong1 < this.v)
      {
        this.v = paramLong1;
        Log.i("IBeaconService", "Adjusted scanStopTime to be " + new Date(this.v));
      }
    }
  }
  
  public void startMonitoringBeaconsInRegion(Region paramRegion, Callback paramCallback)
  {
    synchronized (this.d)
    {
      if (this.d.containsKey(paramRegion))
      {
        Log.i("IBeaconService", "Already monitoring that region -- will replace existing region monitor.");
        this.d.remove(paramRegion);
      }
      this.d.put(paramRegion, new MonitorState(paramCallback));
      if (IBeaconManager.debug) {
        new StringBuilder("Currently monitoring ").append(this.d.size()).append(" regions.");
      }
      if (!this.n) {
        enableScanning();
      }
      return;
    }
  }
  
  public void startRangingBeaconsInRegion(Region paramRegion, Callback paramCallback)
  {
    synchronized (this.c)
    {
      if (this.c.containsKey(paramRegion))
      {
        Log.i("IBeaconService", "Already ranging that region -- will replace existing region.");
        this.c.remove(paramRegion);
      }
      this.c.put(paramRegion, new RangeState(paramCallback));
      if (IBeaconManager.debug) {
        new StringBuilder("Currently ranging ").append(this.c.size()).append(" regions.");
      }
      if (!this.n) {
        enableScanning();
      }
      return;
    }
  }
  
  public void stopMonitoringBeaconsInRegion(Region paramRegion)
  {
    synchronized (this.d)
    {
      this.d.remove(paramRegion);
      if (IBeaconManager.debug) {
        new StringBuilder("Currently monitoring ").append(this.d.size()).append(" regions.");
      }
      if ((this.n) && (this.c.size() == 0) && (this.d.size() == 0)) {
        disableScanning();
      }
      return;
    }
  }
  
  public void stopRangingBeaconsInRegion(Region paramRegion)
  {
    synchronized (this.c)
    {
      this.c.remove(paramRegion);
      if (IBeaconManager.debug) {
        new StringBuilder("Currently ranging ").append(this.c.size()).append(" regions.");
      }
      if ((this.n) && (this.c.size() == 0) && (this.d.size() == 0)) {
        disableScanning();
      }
      return;
    }
  }
  
  public class IBeaconBinder
    extends Binder
  {
    public IBeaconBinder() {}
    
    public IBeaconService getService()
    {
      Log.i("IBeaconService", "getService of IBeaconBinder called");
      return IBeaconService.this;
    }
  }
  
  static final class a
    extends Handler
  {
    private final WeakReference<IBeaconService> a;
    
    a(IBeaconService paramIBeaconService)
    {
      this.a = new WeakReference(paramIBeaconService);
    }
    
    public final void handleMessage(Message paramMessage)
    {
      IBeaconService localIBeaconService = (IBeaconService)this.a.get();
      StartRMData localStartRMData = (StartRMData)paramMessage.obj;
      if (localIBeaconService != null) {}
      switch (paramMessage.what)
      {
      default: 
        super.handleMessage(paramMessage);
        return;
      case 2: 
        Log.i("IBeaconService", "start ranging received");
        localIBeaconService.startRangingBeaconsInRegion(localStartRMData.getRegionData(), new Callback(localStartRMData.getCallbackPackageName()));
        localIBeaconService.setScanPeriods(localStartRMData.getScanPeriod(), localStartRMData.getBetweenScanPeriod());
        return;
      case 3: 
        Log.i("IBeaconService", "stop ranging received");
        localIBeaconService.stopRangingBeaconsInRegion(localStartRMData.getRegionData());
        localIBeaconService.setScanPeriods(localStartRMData.getScanPeriod(), localStartRMData.getBetweenScanPeriod());
        return;
      case 4: 
        Log.i("IBeaconService", "start monitoring received");
        localIBeaconService.startMonitoringBeaconsInRegion(localStartRMData.getRegionData(), new Callback(localStartRMData.getCallbackPackageName()));
        localIBeaconService.setScanPeriods(localStartRMData.getScanPeriod(), localStartRMData.getBetweenScanPeriod());
        return;
      case 5: 
        Log.i("IBeaconService", "stop monitoring received");
        localIBeaconService.stopMonitoringBeaconsInRegion(localStartRMData.getRegionData());
        localIBeaconService.setScanPeriods(localStartRMData.getScanPeriod(), localStartRMData.getBetweenScanPeriod());
        return;
      }
      Log.i("IBeaconService", "set scan intervals received");
      localIBeaconService.setScanPeriods(localStartRMData.getScanPeriod(), localStartRMData.getBetweenScanPeriod());
    }
  }
  
  final class b
  {
    public BluetoothDevice a;
    public int b;
    public byte[] c;
    
    public b(BluetoothDevice paramBluetoothDevice, int paramInt, byte[] paramArrayOfByte)
    {
      this.a = paramBluetoothDevice;
      this.b = paramInt;
      this.c = paramArrayOfByte;
    }
  }
  
  final class c
    extends AsyncTask<IBeaconService.b, Void, Void>
  {
    private c() {}
    
    protected final void onPreExecute() {}
  }
}


/* Location:              /home/merong/decompile/hackery-dex2jar.jar!/com/radiusnetworks/ibeacon/service/IBeaconService.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */