1 /*
2  * Copyright (C) 2016 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 package com.android.cts.verifier.sensors.sixdof.Fragments;
17 
18 
19 import com.android.cts.verifier.sensors.sixdof.Activities.TestActivity;
20 import com.android.cts.verifier.sensors.sixdof.Utils.Manager;
21 import com.android.cts.verifier.sensors.sixdof.Utils.TestReport;
22 import com.android.cts.verifier.sensors.sixdof.Utils.Exceptions.WaypointAreaCoveredException;
23 import com.android.cts.verifier.sensors.sixdof.Utils.Exceptions.WaypointDistanceException;
24 import com.android.cts.verifier.sensors.sixdof.Utils.Exceptions.WaypointRingNotEnteredException;
25 import com.android.cts.verifier.sensors.sixdof.Utils.Exceptions.WaypointStartPointException;
26 import com.android.cts.verifier.sensors.sixdof.Utils.Path.PathUtilityClasses.Ring;
27 import com.android.cts.verifier.sensors.sixdof.Utils.Path.PathUtilityClasses.Waypoint;
28 import com.android.cts.verifier.sensors.sixdof.Utils.PoseProvider.AndroidPoseProvider;
29 import com.android.cts.verifier.sensors.sixdof.Utils.PoseProvider.PoseData;
30 import com.android.cts.verifier.sensors.sixdof.Utils.PoseProvider.PoseProvider;
31 
32 import android.app.Activity;
33 import android.content.Context;
34 import android.app.Fragment;
35 
36 import java.util.ArrayList;
37 
38 /**
39  * This currently deals with the pose data and what to do with it.
40  */
41 public class DataFragment extends Fragment implements PoseProvider.PoseProviderListener {
42     private final static String TAG = "DataFragment";
43 
44     private TestReport mTestReport;
45     private Manager mManager;
46 
47     private PoseProvider mPoseProvider;
48     protected boolean mIsPoseProviderReady = false;
49 
50     @Override
onStart()51     public void onStart() {
52         super.onStart();
53         mPoseProvider = new AndroidPoseProvider(getActivity(), this);
54         mPoseProvider.setup();
55     }
56 
57     @Override
onDestroy()58     public void onDestroy() {
59         super.onDestroy();
60         mPoseProvider = null;
61     }
62 
63     @Override
onPause()64     public void onPause() {
65         super.onPause();
66         mPoseProvider.onStopPoseProviding();
67         mIsPoseProviderReady = false;
68     }
69 
70     /**
71      * Start PoseProvider.
72      */
73     @Override
onSetupComplete()74     public void onSetupComplete() {
75         mPoseProvider.onStartPoseProviding();
76     }
77 
78     @Override
onNewPoseData(PoseData newPoseData)79     public void onNewPoseData(PoseData newPoseData) {
80         if (!mIsPoseProviderReady) {
81             mIsPoseProviderReady = true;
82             mManager.onPoseProviderReady();
83         }
84 
85         mManager.onNewPoseData(newPoseData);
86     }
87 
88     /**
89      * Assign the listener when this fragment is attached to an activity.
90      *
91      * @param activity the activity that this fragment is attached to.
92      */
93     @Override
onAttach(Activity activity)94     public void onAttach(Activity activity) {
95         super.onAttach(activity);
96         initManager(activity);
97     }
98 
initManager(Context context)99     private void initManager(Context context) {
100         mTestReport = new TestReport(getActivity());
101         mManager = new Manager(mTestReport);
102         mManager.setupListeners(context);
103     }
104 
105     /**
106      * Nullify the listener to avoid leaking the activity.
107      */
108     @Override
onDetach()109     public void onDetach() {
110         super.onDetach();
111         mManager.stopListening();
112     }
113 
114     /**
115      * @return PoseProvider object associated with these tests.
116      */
getPoseProvider()117     public PoseProvider getPoseProvider() {
118         return mPoseProvider;
119     }
120 
121     /**
122      * @return true if we are connected to the pose provider.
123      */
isPoseProviderReady()124     public boolean isPoseProviderReady() {
125         return mIsPoseProviderReady;
126     }
127 
128     /**
129      * Gets all the markers (user generated waypoints) for the specified phase.
130      *
131      * @param lap the lap of the test to get the markers from
132      * @return a list of the markers
133      */
getUserGeneratedWaypoints(Manager.Lap lap)134     public ArrayList<Waypoint> getUserGeneratedWaypoints(Manager.Lap lap) {
135         switch (lap) {
136             case LAP_1:
137                 return mManager.getReferencePathMarkers();
138             case LAP_2:
139                 return mManager.getTestPathMarkers();
140             case LAP_3:
141                 return mManager.getRobustnessMarker();
142             case LAP_4:
143                 return mManager.getComplexMovementTestMarkers();
144             default:
145                 throw new AssertionError("Unrecognised Lap!", null);
146         }
147     }
148 
149     /**
150      * Returns a reference to the mTestReport object.
151      */
getTestReport()152     public TestReport getTestReport() {
153         return mTestReport;
154     }
155 
156     /**
157      * Initiates the adding of a waypoint and checks if the state of the current test need to be
158      * changed.
159      *
160      * @throws WaypointDistanceException    if the location is too close to another
161      * @throws WaypointAreaCoveredException if the area covered by the user is too little
162      * @throws WaypointStartPointException  if the location is not close enough to the start
163      */
onWaypointPlacementAttempt()164     public void onWaypointPlacementAttempt()
165             throws WaypointStartPointException, WaypointDistanceException,
166             WaypointAreaCoveredException, WaypointRingNotEnteredException {
167         synchronized (TestActivity.POSE_LOCK) {
168             mManager.addPoseDataToPath(
169                     mPoseProvider.getLatestPoseData().getTranslationAsFloats(), true);
170         }
171     }
172 
173     /**
174      * Removes the last marker added in the current test phase.
175      */
undoWaypointPlacement()176     public void undoWaypointPlacement() {
177         mManager.removeLastAddedMarker();
178     }
179 
180     /**
181      * Returns the current phase of the test.
182      */
getLap()183     public Manager.Lap getLap() {
184         return mManager.getLap();
185     }
186 
187     /**
188      * Sets the test status to executed.
189      */
testStarted()190     public void testStarted() {
191         mTestReport.setTestState(TestReport.TestStatus.EXECUTED);
192     }
193 
startTest(TestActivity.CTSTest newTest)194     public void startTest(TestActivity.CTSTest newTest) {
195         switch (newTest) {
196             case ACCURACY:
197                 mManager.startAccuracyTest();
198                 break;
199             case ROBUSTNESS:
200                 mManager.startRobustnessTest();
201                 break;
202             case COMPLEX_MOVEMENT:
203                 mManager.startComplexMovementTest();
204                 break;
205             default:
206                 throw new AssertionError("Test not recognised!");
207         }
208     }
209 
getLatestDistanceData()210     public float getLatestDistanceData() {
211         return mManager.getRemainingPath();
212     }
213 
getTimeRemaining()214     public float getTimeRemaining() {
215         return mManager.getTimeRemaining();
216     }
217 
getRings()218     public ArrayList<Ring> getRings() {
219         return mManager.getRings();
220     }
221 }
222