Nick0703

Untitled

Jan 13th, 2014
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.43 KB | None | 0 0
  1. /*
  2.  * Copyright (C) 2012 The Android Open Source Project
  3.  * Copyright (C) 2012 Android Open Kang Project
  4.  * Copyright (C) 2013 The SlimRoms Project
  5.  *
  6.  * Licensed under the Apache License, Version 2.0 (the "License");
  7.  * you may not use this file except in compliance with the License.
  8.  * You may obtain a copy of the License at
  9.  *
  10.  *      http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing, software
  13.  * distributed under the License is distributed on an "AS IS" BASIS,
  14.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15.  * See the License for the specific language governing permissions and
  16.  * limitations under the License.
  17.  */
  18.  
  19. package com.android.systemui.quicksettings;
  20.  
  21. import android.content.Context;
  22. import android.content.Intent;
  23. import android.media.MediaPlayer;
  24. import android.media.MediaPlayer.OnCompletionListener;
  25. import android.media.MediaRecorder;
  26. import android.os.Environment;
  27. import android.os.Handler;
  28. import android.util.Log;
  29. import android.view.LayoutInflater;
  30. import android.view.View;
  31. import android.view.View.OnLongClickListener;
  32.  
  33. import com.android.systemui.R;
  34. import com.android.systemui.statusbar.phone.QuickSettingsController;
  35. import com.android.systemui.statusbar.phone.QuickSettingsContainerView;
  36.  
  37. import java.io.File;
  38. import java.io.IOException;
  39.  
  40. public class QuickRecordTile extends QuickSettingsTile {
  41.  
  42.     private static final String TAG = "QuickRecordTile";
  43.  
  44.     public static final int STATE_IDLE = 0;
  45.     public static final int STATE_PLAYING = 1;
  46.     public static final int STATE_RECORDING = 2;
  47.     public static final int STATE_JUST_RECORDED = 3;
  48.     public static final int STATE_NO_RECORDING = 4;
  49.     public static final int MAX_RECORD_TIME = 120000;
  50.  
  51.     private File mFile;
  52.     private Handler mHandler;
  53.     private MediaPlayer mPlayer = null;
  54.     private MediaRecorder mRecorder = null;
  55.     private String mPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getAbsolutePath();
  56.  
  57.     private static String mQuickAudio = null;
  58.  
  59.     private int mRecordingState = 0;
  60.    
  61.     private Context mContext;
  62.  
  63.     public QuickRecordTile(Context context, QuickSettingsController qsc) {
  64.         super(context, qsc);
  65.  
  66.         mContext = context;
  67.         mFile = new File(mPath + File.separator
  68.                 + "quickrecord.3gp");
  69.         mQuickAudio = mFile.getAbsolutePath();
  70.         mHandler = new Handler();
  71.  
  72.         mOnClick = new View.OnClickListener() {
  73.             @Override
  74.             public void onClick(View v) {
  75.                 if (!mFile.exists()) {
  76.                     mRecordingState = STATE_NO_RECORDING;
  77.                 }
  78.                 switch (mRecordingState) {
  79.                     case STATE_RECORDING:
  80.                         stopRecording();
  81.                         break;
  82.                     case STATE_NO_RECORDING:
  83.                         return;
  84.                     case STATE_IDLE:
  85.                     case STATE_JUST_RECORDED:
  86.                         startPlaying();
  87.                         break;
  88.                     case STATE_PLAYING:
  89.                         stopPlaying();
  90.                         break;
  91.                 }
  92.             }
  93.         };
  94.  
  95.         mOnLongClick = new OnLongClickListener() {
  96.             @Override
  97.             public boolean onLongClick(View v) {
  98.                 switch (mRecordingState) {
  99.                     case STATE_NO_RECORDING:
  100.                     case STATE_IDLE:
  101.                     case STATE_JUST_RECORDED:
  102.                         startRecording();
  103.                         break;
  104.                 }
  105.                 return true;
  106.             }
  107.         };
  108.     }
  109.  
  110.     @Override
  111.     void onPostCreate() {
  112.         updateTile();
  113.         super.onPostCreate();
  114.     }
  115.  
  116.     @Override
  117.     public void updateResources() {
  118.         updateTile();
  119.         super.updateResources();
  120.     }
  121.  
  122.     private synchronized void updateTile() {
  123.         int playStateName = 0;
  124.         int playStateIcon = 0;
  125.         if (!mFile.exists()) {
  126.             mRecordingState = STATE_NO_RECORDING;
  127.         }
  128.         switch (mRecordingState) {
  129.             case STATE_IDLE:
  130.                 playStateName = R.string.quick_settings_quick_record_def;
  131.                 playStateIcon = R.drawable.ic_qs_quickrecord;
  132.                 break;
  133.             case STATE_PLAYING:
  134.                 playStateName = R.string.quick_settings_quick_record_play;
  135.                 playStateIcon = R.drawable.ic_qs_playing;
  136.                 break;
  137.             case STATE_RECORDING:
  138.                 playStateName = R.string.quick_settings_quick_record_rec;
  139.                 playStateIcon = R.drawable.ic_qs_recording;
  140.                 break;
  141.             case STATE_JUST_RECORDED:
  142.                 playStateName = R.string.quick_settings_quick_record_save;
  143.                 playStateIcon = R.drawable.ic_qs_saved;
  144.                 break;
  145.             case STATE_NO_RECORDING:
  146.                 playStateName = R.string.quick_settings_quick_record_nofile;
  147.                 playStateIcon = R.drawable.ic_qs_quickrecord;
  148.                 break;
  149.         }
  150.         mDrawable = playStateIcon;
  151.         mLabel = mContext.getString(playStateName);
  152.     }
  153.  
  154.     final Runnable delayTileRevert = new Runnable () {
  155.         public void run() {
  156.             if (mRecordingState == STATE_JUST_RECORDED) {
  157.                 mRecordingState = STATE_IDLE;
  158.                 updateResources();
  159.             }
  160.         }
  161.     };
  162.  
  163.     final Runnable autoStopRecord = new Runnable() {
  164.         public void run() {
  165.             if (mRecordingState == STATE_RECORDING) {
  166.                 stopRecording();
  167.             }
  168.         }
  169.     };
  170.  
  171.     final OnCompletionListener stoppedPlaying = new OnCompletionListener(){
  172.         public void onCompletion(MediaPlayer mp) {
  173.             mRecordingState = STATE_IDLE;
  174.             updateResources();
  175.         }
  176.     };
  177.  
  178.     private void startPlaying() {
  179.         mPlayer = new MediaPlayer();
  180.         try {
  181.             mPlayer.setDataSource(mQuickAudio);
  182.             mPlayer.prepare();
  183.             mPlayer.start();
  184.             mRecordingState = STATE_PLAYING;
  185.             updateResources();
  186.             mPlayer.setOnCompletionListener(stoppedPlaying);
  187.         } catch (IOException e) {
  188.             Log.e(TAG, "QuickRecord prepare() failed on play: ", e);
  189.         }
  190.     }
  191.  
  192.     private void stopPlaying() {
  193.         mPlayer.release();
  194.         mPlayer = null;
  195.         mRecordingState = STATE_IDLE;
  196.         updateResources();
  197.     }
  198.  
  199.     private void startRecording() {
  200.         mRecorder = new MediaRecorder();
  201.         mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
  202.         mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
  203.         mRecorder.setOutputFile(mQuickAudio);
  204.         mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
  205.         try {
  206.             mRecorder.prepare();
  207.             mRecorder.start();
  208.             mRecordingState = STATE_RECORDING;
  209.             updateResources();
  210.             mHandler.postDelayed(autoStopRecord, MAX_RECORD_TIME);
  211.         } catch (IOException e) {
  212.             Log.e(TAG, "QuickRecord prepare() failed on record: ", e);
  213.         }
  214.     }
  215.  
  216.     private void stopRecording() {
  217.         mRecorder.stop();
  218.         mRecorder.release();
  219.         mRecorder = null;
  220.         mRecordingState = STATE_JUST_RECORDED;
  221.         updateResources();
  222.         mHandler.postDelayed(delayTileRevert, 2000);
  223.     }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment