• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import com.android.cts.verifier.R;
19 import com.android.cts.verifier.sensors.sixdof.Activities.StartActivity;
20 import com.android.cts.verifier.sensors.sixdof.Activities.TestActivity;
21 import com.android.cts.verifier.sensors.sixdof.Interfaces.BaseUiListener;
22 import com.android.cts.verifier.sensors.sixdof.Renderer.BaseRenderer;
23 import com.android.cts.verifier.sensors.sixdof.Utils.Manager;
24 
25 import android.app.Activity;
26 import android.content.Intent;
27 import android.os.Handler;
28 import android.app.Fragment;
29 import android.app.AlertDialog;
30 import android.util.Log;
31 import android.view.View;
32 import android.widget.Button;
33 import android.widget.ImageButton;
34 import android.widget.LinearLayout;
35 
36 import java.io.IOException;
37 
38 /**
39  * Abstract class that UI Fragments for each test inherit from,
40  */
41 public abstract class BaseUiFragment extends Fragment implements BaseUiListener {
42     private static final String TAG = "BaseUiFragment";
43     protected static final long UI_UPDATE_DELAY = 200;
44 
45     protected static final int DIALOG_FRAGMENT = 1;
46 
47     protected Button mBtnPass;
48     protected Button mBtnInfo;
49     protected Button mBtnFail;
50     protected ImageButton mPlaceWaypointButton;
51 
52     protected LinearLayout mLLCameraLayout;
53 
54     protected TestActivity mActivity;
55 
56     protected Handler mHandler;
57     protected Runnable mUIUpdateRunnable;
58 
59     protected BaseRenderer mRenderer;
60 
61     /**
62      * Called when this fragment is attached to an activity. Starts the test if the Pose service is
63      * ready.
64      */
65     @Override
onAttach(Activity context)66     public void onAttach(Activity context) {
67         super.onAttach(context);
68         mActivity = (TestActivity) getActivity();
69 
70         if (mActivity.isPoseProviderReady()) {
71             onPoseProviderReady();
72         }
73     }
74 
initUIHandler(Runnable uiRunnable)75     protected void initUIHandler(Runnable uiRunnable) {
76         mHandler = new Handler();
77         mUIUpdateRunnable = uiRunnable;
78         mHandler.postDelayed(mUIUpdateRunnable, UI_UPDATE_DELAY);
79     }
80 
setupButtons(View fragmentView, TestActivity.CTSTest currentPhase)81     protected void setupButtons(View fragmentView, TestActivity.CTSTest currentPhase) {
82         final int phaseIndex = currentPhase.ordinal();
83         mBtnPass = (Button) fragmentView.findViewById(R.id.btnPass);
84         mBtnInfo = (Button) fragmentView.findViewById(R.id.btnInfo);
85         mBtnFail = (Button) fragmentView.findViewById(R.id.btnFail);
86 
87         mBtnInfo.setOnClickListener(new View.OnClickListener() {
88             @Override
89             public void onClick(View view) {
90                 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
91 
92                 builder.setMessage(getResources().getStringArray(R.array.phase_descriptions)[phaseIndex])
93                         .setTitle(getResources().getStringArray(R.array.phase)[phaseIndex])
94                         .setPositiveButton(R.string.got_it, null);
95 
96                 AlertDialog dialog = builder.create();
97                 dialog.show();
98             }
99         });
100 
101         mBtnFail.setOnClickListener(new View.OnClickListener() {
102             @Override
103             public void onClick(View view) {
104                 Intent resultIntent = getActivity().getIntent();
105                 String report = "Couldn't create test report.";
106                 try {
107                     report = mActivity.getTestReport().getContents();
108                 } catch (IOException e) {
109                     Log.e(TAG, report);
110                 }
111                 resultIntent.putExtra(TestActivity.EXTRA_REPORT, report);
112                 resultIntent.putExtra(TestActivity.EXTRA_RESULT_ID, StartActivity.ResultCode.FAILED);
113                 getActivity().setResult(Activity.RESULT_OK, resultIntent);
114                 getActivity().finish();
115             }
116         });
117     }
118 
setupUILoop()119     protected abstract void setupUILoop();
120 
showInitialDialog()121     protected abstract void showInitialDialog();
122 
getObjectiveText(Manager.Lap lap, int waypointCount)123     protected String getObjectiveText(Manager.Lap lap, int waypointCount) {
124         String currentObjective = "";
125         int lapIndex = lap.ordinal();
126         if (lapIndex > 1) lapIndex = 1; // Text is same for indexes 1, 2, 3
127 
128         switch (waypointCount) {
129             case 0:
130                 currentObjective = getResources()
131                         .getStringArray(R.array.initial_waypoint)[lapIndex];
132                 break;
133             case Manager.MAX_MARKER_NUMBER - 1:
134                 currentObjective = getString(R.string.obj_return_to_initial_waypoint);
135                 break;
136             case Manager.MAX_MARKER_NUMBER:
137                 currentObjective = "";
138                 mPlaceWaypointButton.setVisibility(View.INVISIBLE);
139                 break;
140             default:
141                 currentObjective = getResources()
142                         .getStringArray(R.array.next_waypoint)[lapIndex]
143                         .replace('0', Character.forDigit(waypointCount, 10));
144                 break;
145         }
146 
147         return currentObjective;
148     }
149 
150     /**
151      * Nullify activity to avoid memory leak.
152      */
153     @Override
onDetach()154     public void onDetach() {
155         super.onDetach();
156 
157         mActivity = null;
158         mHandler = null;
159         mUIUpdateRunnable = null;
160     }
161 
162     @Override
onDestroyUi()163     public void onDestroyUi() {
164         if (mRenderer != null) {
165             mRenderer.onDestroy();
166         }
167     }
168 
169     @Override
onPause()170     public void onPause() {
171         super.onPause();
172         if (mRenderer != null) {
173             mRenderer.disconnectCamera();
174         }
175     }
176 
177     @Override
onResume()178     public void onResume() {
179         super.onResume();
180         if (mRenderer != null) {
181             mRenderer.connectCamera(mActivity.getPoseProvider(), getActivity());
182         }
183     }
184 
185     @Override
onPoseProviderReady()186     public void onPoseProviderReady() {
187         showInitialDialog();
188         setupUILoop();
189     }
190 
191     /**
192      * Called when a waypoint has been successfully placed by user. Shows undo snackbar.
193      */
194     @Override
onWaypointPlaced()195     public void onWaypointPlaced() {
196         mPlaceWaypointButton.setVisibility(View.INVISIBLE);
197     }
198 }
199