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 java.util.ArrayList; 20 import java.util.List; 21 import com.android.cts.verifier.R; 22 23 import android.content.Context; 24 import android.util.Log; 25 26 /** 27 * The base class for all audio experiments. 28 */ 29 public class Experiment implements Runnable { 30 protected static final String TAG = "AudioQualityVerifier"; 31 32 private static final int DEFAULT_TIMEOUT = 5; // In seconds 33 34 private String mName; 35 private String mScore; 36 private String mReport; 37 private List<String> mAudioFileNames; 38 39 enum Status { NotStarted, Running, Stopped, Completed } 40 private Status mStatus; 41 private boolean mEnabled; 42 43 protected Native mNative; 44 protected Context mContext; 45 protected Terminator mTerminator; 46 47 public Experiment(boolean enable) { 48 mEnabled = enable; 49 mNative = Native.getInstance(); 50 reset(); 51 } 52 53 public void init(Context context) { 54 mName = lookupName(context); 55 } 56 57 protected String lookupName(Context context) { 58 return context.getString(R.string.aq_default_exp); 59 } 60 61 protected String getString(int resId) { 62 return mContext.getString(resId); 63 } 64 65 public boolean isEnabled() { 66 return mEnabled; 67 } 68 69 public void reset() { 70 mStatus = Status.NotStarted; 71 mScore = ""; 72 mReport = ""; 73 mAudioFileNames = new ArrayList<String>(); 74 } 75 76 public void start() { 77 mStatus = Status.Running; 78 } 79 80 protected void setScore(String score) { 81 mScore = score; 82 } 83 84 protected void setReport(String report) { 85 mReport = report; 86 } 87 88 // Implements Runnable 89 public void run() {} 90 91 public void run(Context context, Terminator t) { 92 mContext = context; 93 mTerminator = t; 94 Thread thread = new Thread(this); 95 thread.start(); 96 } 97 98 public void setRecording(byte[] data) { 99 setRecording(data, -1); 100 } 101 102 public void setRecording(byte[] data, int num) { 103 // Save captured data to file 104 String filename = Utils.getExternalDir(mContext, this) + "/" 105 + Utils.cleanString(getName()) 106 + (num == -1 ? "" : "_" + String.valueOf(num)) + ".raw"; 107 Log.i(TAG, "Saving recorded data to " + filename); 108 Utils.saveFile(filename, data); 109 mAudioFileNames.add(filename); 110 } 111 112 public List<String> getAudioFileNames() { 113 return mAudioFileNames; 114 } 115 116 // Timeout in seconds 117 public int getTimeout() { 118 return DEFAULT_TIMEOUT; 119 } 120 121 public void cancel() { 122 mStatus = Status.Stopped; 123 } 124 125 public void stop() { 126 mStatus = Status.Completed; 127 } 128 129 public boolean isRunning() { 130 return mStatus == Status.Running; 131 } 132 133 public String getName() { 134 return mName; 135 } 136 137 public String getScore() { 138 switch (mStatus) { 139 case NotStarted: 140 return "-"; 141 case Running: 142 return "..."; 143 case Stopped: 144 return "-"; 145 case Completed: 146 return mScore; 147 } 148 return ""; 149 } 150 151 public void getReport(StringBuilder sb) { 152 sb.append(getName()); 153 sb.append(": "); 154 sb.append(getScore()); 155 sb.append("\n"); 156 sb.append(mReport); 157 sb.append("\n\n"); 158 } 159 } 160