IBeaconManager.java 14.2 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
package com.radiusnetworks.ibeacon;

import android.annotation.TargetApi;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.Build.VERSION;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;
import com.radiusnetworks.ibeacon.service.IBeaconService;
import com.radiusnetworks.ibeacon.service.RegionData;
import com.radiusnetworks.ibeacon.service.StartRMData;
import com.radiusnetworks.ibeacon.simulator.BeaconSimulator;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

@TargetApi(4)
public class IBeaconManager
{
  public static final long DEFAULT_BACKGROUND_BETWEEN_SCAN_PERIOD = 300000L;
  public static final long DEFAULT_BACKGROUND_SCAN_PERIOD = 10000L;
  public static final long DEFAULT_FOREGROUND_BETWEEN_SCAN_PERIOD = 0L;
  public static final long DEFAULT_FOREGROUND_SCAN_PERIOD = 1100L;
  protected static BeaconSimulator beaconSimulator;
  protected static IBeaconManager client = null;
  public static boolean debug = false;
  private Context a;
  private Map<IBeaconConsumer, a> b = new HashMap();
  private Messenger c = null;
  private ArrayList<Region> d = new ArrayList();
  protected RangeNotifier dataRequestNotifier = null;
  private ArrayList<Region> e = new ArrayList();
  private long f = 1100L;
  private long g = 0L;
  private long h = 10000L;
  private long i = 300000L;
  private ServiceConnection j = new ServiceConnection()
  {
    public final void onServiceConnected(ComponentName arg1, IBinder paramAnonymousIBinder)
    {
      IBeaconManager.a(IBeaconManager.this, new Messenger(paramAnonymousIBinder));
      synchronized (IBeaconManager.a(IBeaconManager.this))
      {
        paramAnonymousIBinder = IBeaconManager.a(IBeaconManager.this).keySet().iterator();
        while (paramAnonymousIBinder.hasNext())
        {
          IBeaconConsumer localIBeaconConsumer = (IBeaconConsumer)paramAnonymousIBinder.next();
          if (!Boolean.valueOf(((IBeaconManager.a)IBeaconManager.a(IBeaconManager.this).get(localIBeaconConsumer)).a).booleanValue())
          {
            localIBeaconConsumer.onIBeaconServiceConnect();
            IBeaconManager.a locala = (IBeaconManager.a)IBeaconManager.a(IBeaconManager.this).get(localIBeaconConsumer);
            locala.a = true;
            IBeaconManager.a(IBeaconManager.this).put(localIBeaconConsumer, locala);
          }
        }
      }
    }
    
    public final void onServiceDisconnected(ComponentName paramAnonymousComponentName)
    {
      Log.e("IBeaconManager", "onServiceDisconnected");
    }
  };
  protected MonitorNotifier monitorNotifier = null;
  protected RangeNotifier rangeNotifier = null;
  
  protected IBeaconManager(Context paramContext)
  {
    this.a = paramContext;
  }
  
  private boolean a()
  {
    boolean bool = true;
    label127:
    for (;;)
    {
      synchronized (this.b)
      {
        Iterator localIterator = this.b.keySet().iterator();
        if (localIterator.hasNext())
        {
          IBeaconConsumer localIBeaconConsumer = (IBeaconConsumer)localIterator.next();
          if (!((a)this.b.get(localIBeaconConsumer)).b)
          {
            bool = false;
            if (!debug) {
              break label127;
            }
            new StringBuilder("Consumer ").append(localIBeaconConsumer).append(" isInBackground=").append(((a)this.b.get(localIBeaconConsumer)).b);
            break label127;
          }
        }
        else
        {
          return bool;
        }
      }
    }
  }
  
  private long b()
  {
    if (a()) {
      return this.h;
    }
    return this.f;
  }
  
  private long c()
  {
    if (a()) {
      return this.i;
    }
    return this.g;
  }
  
  public static BeaconSimulator getBeaconSimulator()
  {
    return beaconSimulator;
  }
  
  public static IBeaconManager getInstanceForApplication(Context paramContext)
  {
    if (client == null) {
      client = new IBeaconManager(paramContext);
    }
    return client;
  }
  
  public static void setBeaconSimulator(BeaconSimulator paramBeaconSimulator)
  {
    beaconSimulator = paramBeaconSimulator;
  }
  
  public static void setDebug(boolean paramBoolean)
  {
    debug = paramBoolean;
  }
  
  public void bind(IBeaconConsumer paramIBeaconConsumer)
  {
    if (Build.VERSION.SDK_INT < 18)
    {
      Log.w("IBeaconManager", "Not supported prior to SDK 18.  Method invocation will be ignored");
      return;
    }
    for (;;)
    {
      synchronized (this.b)
      {
        if (this.b.keySet().contains(paramIBeaconConsumer))
        {
          if (debug) {}
          return;
        }
      }
      if (debug) {
        new StringBuilder("This consumer is not bound.  binding: ").append(paramIBeaconConsumer);
      }
      this.b.put(paramIBeaconConsumer, new a((byte)0));
      paramIBeaconConsumer.bindService(new Intent(paramIBeaconConsumer.getApplicationContext(), IBeaconService.class), this.j, 1);
      if (debug) {
        new StringBuilder("consumer count is now:").append(this.b.size());
      }
      if (this.c != null) {
        setBackgroundMode(paramIBeaconConsumer, false);
      }
    }
  }
  
  @TargetApi(18)
  public boolean checkAvailability()
    throws BleNotAvailableException
  {
    if (Build.VERSION.SDK_INT < 18) {
      throw new BleNotAvailableException("Bluetooth LE not supported by this device");
    }
    if (!this.a.getPackageManager().hasSystemFeature("android.hardware.bluetooth_le")) {
      throw new BleNotAvailableException("Bluetooth LE not supported by this device");
    }
    return ((BluetoothManager)this.a.getSystemService("bluetooth")).getAdapter().isEnabled();
  }
  
  protected RangeNotifier getDataRequestNotifier()
  {
    return this.dataRequestNotifier;
  }
  
  public Collection<Region> getMonitoredRegions()
  {
    ArrayList localArrayList2 = new ArrayList();
    synchronized (this.d)
    {
      Iterator localIterator = this.d.iterator();
      if (localIterator.hasNext()) {
        localArrayList2.add((Region)((Region)localIterator.next()).clone());
      }
    }
    return localCollection;
  }
  
  public MonitorNotifier getMonitoringNotifier()
  {
    return this.monitorNotifier;
  }
  
  public Collection<Region> getRangedRegions()
  {
    ArrayList localArrayList2 = new ArrayList();
    synchronized (this.e)
    {
      Iterator localIterator = this.e.iterator();
      if (localIterator.hasNext()) {
        localArrayList2.add((Region)((Region)localIterator.next()).clone());
      }
    }
    return localCollection;
  }
  
  public RangeNotifier getRangingNotifier()
  {
    return this.rangeNotifier;
  }
  
  public boolean isBound(IBeaconConsumer paramIBeaconConsumer)
  {
    for (;;)
    {
      synchronized (this.b)
      {
        if ((this.b.keySet().contains(paramIBeaconConsumer)) && (this.c != null))
        {
          bool = true;
          return bool;
        }
      }
      boolean bool = false;
    }
  }
  
  public void setBackgroundBetweenScanPeriod(long paramLong)
  {
    this.i = paramLong;
  }
  
  public boolean setBackgroundMode(IBeaconConsumer paramIBeaconConsumer, boolean paramBoolean)
  {
    if (Build.VERSION.SDK_INT < 18)
    {
      Log.w("IBeaconManager", "Not supported prior to SDK 18.  Method invocation will be ignored");
      return false;
    }
    synchronized (this.b)
    {
      Log.i("IBeaconManager", "setBackgroundMode for consumer" + paramIBeaconConsumer + " to " + paramBoolean);
      boolean bool = this.b.keySet().contains(paramIBeaconConsumer);
      if (!bool) {}
    }
    if (debug) {
      new StringBuilder("This consumer is not bound to: ").append(paramIBeaconConsumer);
    }
    return false;
  }
  
  public void setBackgroundScanPeriod(long paramLong)
  {
    this.h = paramLong;
  }
  
  protected void setDataRequestNotifier(RangeNotifier paramRangeNotifier)
  {
    this.dataRequestNotifier = paramRangeNotifier;
  }
  
  public void setForegroundBetweenScanPeriod(long paramLong)
  {
    this.g = paramLong;
  }
  
  public void setForegroundScanPeriod(long paramLong)
  {
    this.f = paramLong;
  }
  
  public void setMonitorNotifier(MonitorNotifier paramMonitorNotifier)
  {
    this.monitorNotifier = paramMonitorNotifier;
  }
  
  public void setRangeNotifier(RangeNotifier paramRangeNotifier)
  {
    this.rangeNotifier = paramRangeNotifier;
  }
  
  public void setScanPeriods()
    throws RemoteException
  {
    updateScanPeriods();
  }
  
  @TargetApi(18)
  public void startMonitoringBeaconsInRegion(Region paramRegion)
    throws RemoteException
  {
    if (Build.VERSION.SDK_INT < 18)
    {
      Log.w("IBeaconManager", "Not supported prior to API 18.  Method invocation will be ignored");
      return;
    }
    if (this.c == null) {
      throw new RemoteException("The IBeaconManager is not bound to the service.  Call iBeaconManager.bind(IBeaconConsumer consumer) and wait for a callback to onIBeaconServiceConnect()");
    }
    ??? = Message.obtain(null, 4, 0, 0);
    ((Message)???).obj = new StartRMData(new RegionData(paramRegion), this.a.getPackageName(), b(), c());
    this.c.send((Message)???);
    synchronized (this.d)
    {
      this.d.add((Region)paramRegion.clone());
      return;
    }
  }
  
  @TargetApi(18)
  public void startRangingBeaconsInRegion(Region paramRegion)
    throws RemoteException
  {
    if (Build.VERSION.SDK_INT < 18)
    {
      Log.w("IBeaconManager", "Not supported prior to SDK 18.  Method invocation will be ignored");
      return;
    }
    if (this.c == null) {
      throw new RemoteException("The IBeaconManager is not bound to the service.  Call iBeaconManager.bind(IBeaconConsumer consumer) and wait for a callback to onIBeaconServiceConnect()");
    }
    ??? = Message.obtain(null, 2, 0, 0);
    ((Message)???).obj = new StartRMData(new RegionData(paramRegion), this.a.getPackageName(), b(), c());
    this.c.send((Message)???);
    synchronized (this.e)
    {
      this.e.add((Region)paramRegion.clone());
      return;
    }
  }
  
  @TargetApi(18)
  public void stopMonitoringBeaconsInRegion(Region paramRegion)
    throws RemoteException
  {
    if (Build.VERSION.SDK_INT < 18)
    {
      Log.w("IBeaconManager", "Not supported prior to API 18.  Method invocation will be ignored");
      return;
    }
    if (this.c == null) {
      throw new RemoteException("The IBeaconManager is not bound to the service.  Call iBeaconManager.bind(IBeaconConsumer consumer) and wait for a callback to onIBeaconServiceConnect()");
    }
    Object localObject = Message.obtain(null, 5, 0, 0);
    ((Message)localObject).obj = new StartRMData(new RegionData(paramRegion), this.a.getPackageName(), b(), c());
    this.c.send((Message)localObject);
    for (;;)
    {
      synchronized (this.d)
      {
        Iterator localIterator = this.d.iterator();
        localObject = null;
        if (localIterator.hasNext())
        {
          Region localRegion = (Region)localIterator.next();
          if (paramRegion.getUniqueId().equals(localRegion.getUniqueId())) {
            localObject = localRegion;
          }
        }
        else
        {
          this.d.remove(localObject);
          return;
        }
      }
    }
  }
  
  @TargetApi(18)
  public void stopRangingBeaconsInRegion(Region paramRegion)
    throws RemoteException
  {
    if (Build.VERSION.SDK_INT < 18)
    {
      Log.w("IBeaconManager", "Not supported prior to SDK 18.  Method invocation will be ignored");
      return;
    }
    if (this.c == null) {
      throw new RemoteException("The IBeaconManager is not bound to the service.  Call iBeaconManager.bind(IBeaconConsumer consumer) and wait for a callback to onIBeaconServiceConnect()");
    }
    Object localObject = Message.obtain(null, 3, 0, 0);
    ((Message)localObject).obj = new StartRMData(new RegionData(paramRegion), this.a.getPackageName(), b(), c());
    this.c.send((Message)localObject);
    for (;;)
    {
      synchronized (this.e)
      {
        Iterator localIterator = this.e.iterator();
        localObject = null;
        if (localIterator.hasNext())
        {
          Region localRegion = (Region)localIterator.next();
          if (paramRegion.getUniqueId().equals(localRegion.getUniqueId())) {
            localObject = localRegion;
          }
        }
        else
        {
          this.e.remove(localObject);
          return;
        }
      }
    }
  }
  
  public void unBind(IBeaconConsumer paramIBeaconConsumer)
  {
    if (Build.VERSION.SDK_INT < 18)
    {
      Log.w("IBeaconManager", "Not supported prior to SDK 18.  Method invocation will be ignored");
      return;
    }
    for (;;)
    {
      synchronized (this.b)
      {
        if (this.b.keySet().contains(paramIBeaconConsumer))
        {
          paramIBeaconConsumer.unbindService(this.j);
          this.b.remove(paramIBeaconConsumer);
          return;
        }
      }
      if (debug) {
        new StringBuilder("This consumer is not bound to: ").append(paramIBeaconConsumer);
      }
      int k;
      while (k < this.b.size())
      {
        Log.i("IBeaconManager", " " + this.b.get(Integer.valueOf(k)));
        k += 1;
        continue;
        k = 0;
      }
    }
  }
  
  @TargetApi(18)
  public void updateScanPeriods()
    throws RemoteException
  {
    if (Build.VERSION.SDK_INT < 18)
    {
      Log.w("IBeaconManager", "Not supported prior to API 18.  Method invocation will be ignored");
      return;
    }
    if (this.c == null) {
      throw new RemoteException("The IBeaconManager is not bound to the service.  Call iBeaconManager.bind(IBeaconConsumer consumer) and wait for a callback to onIBeaconServiceConnect()");
    }
    Message localMessage = Message.obtain(null, 6, 0, 0);
    new StringBuilder("updating scan period to ").append(b()).append(", ").append(c());
    localMessage.obj = new StartRMData(b(), c());
    this.c.send(localMessage);
  }
  
  final class a
  {
    public boolean a = false;
    public boolean b = false;
    
    private a() {}
  }
}


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