1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 
17 #include <stdlib.h>
18 #include "Log.h"
19 #include "StringUtil.h"
20 #include "task/TaskAll.h"
21 
TaskAsync(TaskType type)22 TaskAsync::TaskAsync(TaskType type)
23     : TaskGeneric(type),
24       mVolume(-1),
25       mDeviceType(EDeviceHost),
26       mMode(AudioHardware::EModeVoice),
27       mAsynchronous(false)
28 {
29     // nothing to do
30 }
31 
~TaskAsync()32 TaskAsync::~TaskAsync()
33 {
34 
35 }
36 
run()37 TaskGeneric::ExecutionResult TaskAsync::run()
38 {
39     // id is mandatory
40     if (mId.length() == 0) {
41         LOGE(" TaskAsync::run no id attribute");
42         return TaskGeneric::EResultError;
43     }
44     TaskGeneric::ExecutionResult result = start();
45     if (result == TaskGeneric::EResultOK) {
46         if (!isAsynchronous()) {
47             return complete();
48         } else {
49             if (!getParentSequential()->queueAsyncTask(const_cast<TaskAsync*>(this))) {
50                 LOGE("TaskAsync::run queueAsyncTask failed");
51                 return TaskGeneric::EResultError;
52             }
53         }
54     }
55     return result;
56 }
57 
parseAttribute(const android::String8 & name,const android::String8 & value)58 bool TaskAsync::parseAttribute(const android::String8& name, const android::String8& value)
59 {
60     bool result = true;
61     if (StringUtil::compare(name, "id") == 0) {
62         mId.append(value);
63     } else if (StringUtil::compare(name, "gain") == 0) {
64         mVolume = atoi(value.string());
65         if ((mVolume < 1) || (mVolume > 100)) {
66             LOGE("TaskGeneric::parseAttribute gain out of range %d", mVolume);
67             return false;
68         }
69     } else if (StringUtil::compare(name, "sync") == 0) {
70         if (StringUtil::compare(value, "start") == 0) { // async
71             makeAsynchronous();
72         }
73     } else if (StringUtil::compare(name, "device") == 0) {
74         if (StringUtil::compare(value, "host") == 0) {
75             mDeviceType = EDeviceHost;
76         } else if (StringUtil::compare(value, "DUT") == 0) {
77             mDeviceType = EDeviceDUT;
78         } else {
79             return false;
80         }
81     } else if (StringUtil::compare(name, "mode") == 0) {
82             if (StringUtil::compare(value, "voice") == 0) {
83                 mMode = AudioHardware::EModeVoice;
84             } else if (StringUtil::compare(value, "music") == 0) {
85                 mMode = AudioHardware::EModeMusic;
86             } else {
87                 return false;
88             }
89     } else {
90         result = TaskGeneric::parseAttribute(name, value);
91     }
92     return result;
93 }
94 
getParentSequential()95 TaskSequential* TaskAsync::getParentSequential()
96 {
97     ASSERT(getParent()->getType() == TaskGeneric::ETaskSequential);
98     return reinterpret_cast<TaskSequential*>(getParent());
99 }
100 
101