content: /*
   * Copyright 2016, The Android Open Source Project
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package com.example.androidthings.driversamples;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.os.Handler;
  import android.os.HandlerThread;
  import android.util.Log;
  
  import com.google.android.things.contrib.driver.pwmspeaker.Speaker;
  
  import java.io.IOException;
  
  import static android.content.ContentValues.TAG;
  
  public class SpeakerActivity extends Activity {
  
      private static final long PLAYBACK_NOTE_DELAY = 80L;
  
      private Speaker mSpeaker;
      private HandlerThread mHandlerThread;
      private Handler mHandler;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          try {
              mSpeaker = new Speaker(BoardDefaults.getPwmPin());
              mSpeaker.stop(); // in case the PWM pin was enabled already
          } catch (IOException e) {
              Log.e(TAG, "Error initializing speaker");
              return; // don't initilize the handler
          }
  
          mHandlerThread = new HandlerThread("pwm-playback");
          mHandlerThread.start();
          mHandler = new Handler(mHandlerThread.getLooper());
          mHandler.post(mPlaybackRunnable);
      }
  
      @Override
      protected void onDestroy() {
          super.onDestroy();
  
          if (mHandler != null) {
              mHandler.removeCallbacks(mPlaybackRunnable);
              mHandlerThread.quitSafely();
          }
          if (mSpeaker != null) {
              try {
                  mSpeaker.stop();
                  mSpeaker.close();
              } catch (IOException e) {
                  Log.e(TAG, "Error closing speaker", e);
              } finally {
                  mSpeaker = null;
              }
          }
      }
  
      private Runnable mPlaybackRunnable = new Runnable() {
  
          private int index = 0;
  
          @Override
          public void run() {
              if (mSpeaker == null) {
                  return;
              }
  
              try {
                  if (index == MusicNotes.DRAMATIC_THEME.length) {
                      // reached the end
                      mSpeaker.stop();
                  } else {
                      double note = MusicNotes.DRAMATIC_THEME[index++];
                      if (note > 0) {
                          mSpeaker.play(note);
                      } else {
                          mSpeaker.stop();
                      }
                      mHandler.postDelayed(this, PLAYBACK_NOTE_DELAY);
                  }
              } catch (IOException e) {
                  Log.e(TAG, "Error playing speaker", e);
              }
          }
      };
  }
  