1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.cts.verifier.audioquality; 18 19 import com.android.cts.verifier.R; 20 21 import android.content.Context; 22 import android.util.Log; 23 24 /** 25 * The base class for all audio experiments. 26 */ 27 public class Experiment implements Runnable { 28 protected static final String TAG = "AudioQualityVerifier"; 29 30 private static final int DEFAULT_TIMEOUT = 5; // In seconds 31 32 private String mName; 33 private String mScore; 34 private String mReport; 35 private String mAudioFileName; 36 37 enum Status { NotStarted, Running, Stopped, Completed } 38 private Status mStatus; 39 private boolean mEnabled; 40 41 protected Native mNative; 42 protected Context mContext; 43 protected Terminator mTerminator; 44 45 public Experiment(boolean enable) { 46 mEnabled = enable; 47 mNative = Native.getInstance(); 48 reset(); 49 } 50 51 public void init(Context context) { 52 mName = lookupName(context); 53 } 54 55 protected String lookupName(Context context) { 56 return context.getString(R.string.aq_default_exp); 57 } 58 59 protected String getString(int resId) { 60 return mContext.getString(resId); 61 } 62 63 public boolean isEnabled() { 64 return mEnabled; 65 } 66 67 public void reset() { 68 mStatus = Status.NotStarted; 69 mScore = ""; 70 mReport = ""; 71 mAudioFileName = null; 72 } 73 74 public void start() { 75 mStatus = Status.Running; 76 } 77 78 protected void setScore(String score) { 79 mScore = score; 80 } 81 82 protected void setReport(String report) { 83 mReport = report; 84 } 85 86 // Implements Runnable 87 public void run() {} 88 89 public void run(Context context, Terminator t) { 90 mContext = context; 91 mTerminator = t; 92 Thread thread = new Thread(this); 93 thread.start(); 94 } 95 96 public void setRecording(byte[] data) { 97 // Save captured data to file 98 mAudioFileName = Utils.getExternalDir(mContext, this) + "/" 99 + Utils.cleanString(getName()) + ".raw"; 100 Log.i(TAG, "Saving recorded data to " + mAudioFileName); 101 Utils.saveFile(mAudioFileName, data); 102 } 103 104 public String getAudioFileName() { 105 return mAudioFileName; 106 } 107 108 // Timeout in seconds 109 public int getTimeout() { 110 return DEFAULT_TIMEOUT; 111 } 112 113 public void cancel() { 114 mStatus = Status.Stopped; 115 } 116 117 public void stop() { 118 mStatus = Status.Completed; 119 } 120 121 public boolean isRunning() { 122 return mStatus == Status.Running; 123 } 124 125 public String getName() { 126 return mName; 127 } 128 129 public String getScore() { 130 switch (mStatus) { 131 case NotStarted: 132 return "-"; 133 case Running: 134 return "..."; 135 case Stopped: 136 return "-"; 137 case Completed: 138 return mScore; 139 } 140 return ""; 141 } 142 143 public void getReport(StringBuilder sb) { 144 sb.append(getName()); 145 sb.append(": "); 146 sb.append(getScore()); 147 sb.append("\n"); 148 sb.append(mReport); 149 sb.append("\n\n"); 150 } 151 } 152