예제 #1
0
public class VolumeHelper {
  private static final int DEFAULT_VALUE_BCT = 9;
  private static final int DEFAULT_VALUE_EARBUD = 9;
  private static final String EXTRA_HEADSET_STATE = "state";
  public static final int HEADSET_STATE_PLUGGED = 1;
  public static final int HEADSET_STATE_UNPLUGGED = 0;
  private static final int NUM_VALUES_BCT = 10;
  private static final int NUM_VALUES_EARBUDS = 19;
  private static final FormattingLogger logger = FormattingLoggers.getContextLogger();
  private final AudioManager audioManager;

  public VolumeHelper(Context paramContext, AudioManager paramAudioManager) {
    this.audioManager = paramAudioManager;
  }

  public static int getHeadsetState(Context paramContext) {
    return getHeadsetState(
        paramContext.registerReceiver(
            null, new IntentFilter("android.intent.action.HEADSET_PLUG")));
  }

  public static int getHeadsetState(Intent paramIntent) {
    if (paramIntent == null) {
      logger.d("Defaulting to headset state: 0", new Object[0]);
      return 0;
    }
    int i = paramIntent.getIntExtra("state", 0);
    FormattingLogger localFormattingLogger = logger;
    Object[] arrayOfObject = new Object[1];
    arrayOfObject[0] = Integer.valueOf(i);
    localFormattingLogger.d("Determined headset state: %s", arrayOfObject);
    return i;
  }

  public static int getNumVolumeValues(int paramInt) {
    if (paramInt == 1) return 19;
    return 10;
  }

  public int readAudioVolume(int paramInt) {
    try {
      int j = Integer.parseInt(this.audioManager.getParameters("earbuds_volume"));
      FormattingLogger localFormattingLogger2 = logger;
      Object[] arrayOfObject2 = new Object[1];
      arrayOfObject2[0] = Integer.valueOf(j);
      localFormattingLogger2.d("Read volume from HAL: %s", arrayOfObject2);
      return j;
    } catch (NumberFormatException localNumberFormatException) {
      if (paramInt != 1) ;
    }
    for (int i = 9; ; i = 9) {
      FormattingLogger localFormattingLogger1 = logger;
      Object[] arrayOfObject1 = new Object[1];
      arrayOfObject1[0] = Integer.valueOf(i);
      localFormattingLogger1.w(
          localNumberFormatException,
          "Could not read HAL volume, defaulting to: %s",
          arrayOfObject1);
      return i;
    }
  }

  public void writeAudioVolume(int paramInt1, int paramInt2) {
    FormattingLogger localFormattingLogger = logger;
    Object[] arrayOfObject = new Object[1];
    arrayOfObject[0] = Integer.valueOf(paramInt2);
    localFormattingLogger.d("Writing volume to HAL: %s", arrayOfObject);
    this.audioManager.setParameters("earbuds_volume=" + paramInt2);
  }
}
예제 #2
0
public class ProtoListFile<T extends MessageNano> {
  private static final long TIME_BETWEEN_SAVES_MS = TimeUnit.MINUTES.toMillis(1L);
  private static final FormattingLogger logger = FormattingLoggers.getContextLogger();
  private final Context context;
  private final ScheduledExecutorService executor;
  private final ProtoParser<T> parser;
  private final String path;
  private List<T> saveQueue = new ArrayList();

  public ProtoListFile(
      Context paramContext,
      String paramString,
      ScheduledExecutorService paramScheduledExecutorService,
      ProtoParser<T> paramProtoParser) {
    this.context = paramContext;
    this.path = paramString;
    this.executor = paramScheduledExecutorService;
    this.parser = paramProtoParser;
  }

  private T readProto(DataInputStream paramDataInputStream) throws IOException {
    int i = paramDataInputStream.readInt();
    byte[] arrayOfByte = new byte[i];
    paramDataInputStream.readFully(arrayOfByte, 0, i);
    return this.parser.parse(arrayOfByte);
  }

  private void writeProto(T paramT, DataOutputStream paramDataOutputStream) throws IOException {
    byte[] arrayOfByte = MessageNano.toByteArray(paramT);
    paramDataOutputStream.writeInt(arrayOfByte.length);
    paramDataOutputStream.write(arrayOfByte, 0, arrayOfByte.length);
  }

  public void delete() {
    Assert.assertNotUiThread();
    this.context.deleteFile(this.path);
    this.saveQueue.clear();
  }

  public void flushWrites() {
    Assert.assertNotUiThread();
    if (!this.saveQueue.isEmpty()) {
      FileOutputStream localFileOutputStream = null;
      try {
        localFileOutputStream = this.context.openFileOutput(this.path, 32768);
        DataOutputStream localDataOutputStream =
            new DataOutputStream(new BufferedOutputStream(localFileOutputStream));
        int i = 0;
        int j = this.saveQueue.size();
        while (i < j) {
          writeProto((MessageNano) this.saveQueue.get(i), localDataOutputStream);
          i++;
        }
        localDataOutputStream.flush();
        logger.i("Saved " + this.saveQueue.size() + " proto(s) to disk.", new Object[0]);
        this.saveQueue.clear();
        if (localFileOutputStream != null) ;
        try {
          localFileOutputStream.close();
          return;
        } catch (IOException localIOException4) {
          logger.w("Could not close protos file after write.", new Object[] {localIOException4});
          return;
        }
      } catch (IOException localIOException2) {
        do {
          logger.w("Could not write protos.", new Object[] {localIOException2});
          this.saveQueue.clear();
        } while (localFileOutputStream == null);
        try {
          localFileOutputStream.close();
          return;
        } catch (IOException localIOException3) {
          logger.w("Could not close protos file after write.", new Object[] {localIOException3});
          return;
        }
      } finally {
        this.saveQueue.clear();
        if (localFileOutputStream == null) ;
      }
      try {
        localFileOutputStream.close();
        throw localObject;
      } catch (IOException localIOException1) {
        while (true)
          logger.w("Could not close protos file after write.", new Object[] {localIOException1});
      }
    }
    logger.i("No protos to save to disk.", new Object[0]);
  }

  public long flushedSize() {
    Assert.assertNotUiThread();
    return this.context.getFileStreamPath(this.path).length();
  }

  public List<T> read() {
    Assert.assertNotUiThread();
    FileInputStream localFileInputStream = null;
    try {
      localFileInputStream = this.context.openFileInput(this.path);
      DataInputStream localDataInputStream =
          new DataInputStream(new BufferedInputStream(localFileInputStream));
      localArrayList = new ArrayList();
      int i = 1;
      while (i != 0)
        try {
          localArrayList.add(readProto(localDataInputStream));
        } catch (EOFException localEOFException) {
          i = 0;
        }
      logger.i("Read " + localArrayList.size() + " persisted protos.", new Object[0]);
      if (localArrayList.isEmpty()) this.context.deleteFile(this.path);
      localArrayList.addAll(this.saveQueue);
      if (localFileInputStream != null) ;
      try {
        localFileInputStream.close();
        return localArrayList;
      } catch (IOException localIOException4) {
        logger.w("Could not close file after read.", new Object[] {localIOException4});
        return localArrayList;
      }
    } catch (IOException localIOException2) {
      ArrayList localArrayList;
      do {
        logger.d("Could not read protos from disk.", new Object[] {localIOException2});
        this.context.deleteFile(this.path);
        localArrayList = new ArrayList(this.saveQueue);
      } while (localFileInputStream == null);
      try {
        localFileInputStream.close();
        return localArrayList;
      } catch (IOException localIOException3) {
        logger.w("Could not close file after read.", new Object[] {localIOException3});
        return localArrayList;
      }
    } finally {
      if (localFileInputStream == null) ;
    }
    try {
      localFileInputStream.close();
      throw localObject;
    } catch (IOException localIOException1) {
      while (true) logger.w("Could not close file after read.", new Object[] {localIOException1});
    }
  }

  public void write(T paramT) {
    Assert.assertNotUiThread();
    if (this.saveQueue.isEmpty())
      this.executor.schedule(
          new Runnable() {
            public void run() {
              ProtoListFile.this.flushWrites();
            }
          },
          TIME_BETWEEN_SAVES_MS,
          TimeUnit.MILLISECONDS);
    this.saveQueue.add(paramT);
  }
}