InstrumentClusterFragment.java revision 46371473c416415fb6bcb8db85686669c3d65af6
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 */
16package com.google.android.car.kitchensink.cluster;
17
18import android.app.AlertDialog;
19import android.os.Bundle;
20import android.support.annotation.Nullable;
21import android.support.car.CarAppFocusManager;
22import android.support.car.CarAppFocusManager.AppFocusChangeListener;
23import android.support.car.CarAppFocusManager.AppFocusOwnershipChangeListener;
24import android.support.car.CarNotConnectedException;
25import android.support.car.navigation.CarNavigationStatusManager;
26import android.support.v4.app.Fragment;
27import android.util.Log;
28import android.view.LayoutInflater;
29import android.view.View;
30import android.view.ViewGroup;
31
32import com.google.android.car.kitchensink.R;
33
34/**
35 * Contains functions to test instrument cluster API.
36 */
37public class InstrumentClusterFragment extends Fragment {
38    private static final String TAG = InstrumentClusterFragment.class.getSimpleName();
39
40    private CarNavigationStatusManager mCarNavigationStatusManager;
41    private CarAppFocusManager mCarAppFocusManager;
42
43    public void setCarNavigationStatusManager(
44            CarNavigationStatusManager carNavigationStatusManager) {
45        mCarNavigationStatusManager = carNavigationStatusManager;
46    }
47
48    public void setCarAppFocusManager(CarAppFocusManager carAppFocusManager) {
49        mCarAppFocusManager = carAppFocusManager;
50    }
51
52    @Nullable
53    @Override
54    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
55            @Nullable Bundle savedInstanceState) {
56        View view = inflater.inflate(R.layout.instrument_cluster, container, false);
57
58        view.findViewById(R.id.cluster_start_button).setOnClickListener(v -> initCluster());
59        view.findViewById(R.id.cluster_turn_left_button).setOnClickListener(v -> turnLeft());
60
61        return super.onCreateView(inflater, container, savedInstanceState);
62    }
63
64    private void turnLeft() {
65        try {
66            mCarNavigationStatusManager
67                    .sendNavigationTurnEvent(CarNavigationStatusManager.TURN_TURN, "Huff Ave", 90,
68                            -1, null, CarNavigationStatusManager.TURN_SIDE_LEFT);
69            mCarNavigationStatusManager.sendNavigationTurnDistanceEvent(500, 10, 500,
70                    CarNavigationStatusManager.DISTANCE_METERS);
71        } catch (CarNotConnectedException e) {
72            e.printStackTrace();
73        }
74    }
75
76    private void initCluster() {
77        try {
78            mCarAppFocusManager.registerFocusListener(new AppFocusChangeListener() {
79                @Override
80                public void onAppFocusChange(int appType, boolean active) {
81                    Log.d(TAG, "onAppFocusChange, appType: " + appType + " active: " + active);
82                }
83            }, CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION);
84        } catch (CarNotConnectedException e) {
85            Log.e(TAG, "Failed to register focus listener", e);
86        }
87
88        AppFocusOwnershipChangeListener focusListener = new AppFocusOwnershipChangeListener() {
89            @Override
90            public void onAppFocusOwnershipLoss(int focus) {
91                Log.w(TAG, "onAppFocusOwnershipLoss, focus: " + focus);
92                new AlertDialog.Builder(getContext())
93                        .setTitle(getContext().getApplicationInfo().name)
94                        .setMessage(R.string.cluster_nav_app_context_loss)
95                        .show();
96            }
97        };
98        try {
99            mCarAppFocusManager.requestAppFocus(focusListener,
100                CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION);
101        } catch (CarNotConnectedException e) {
102            Log.e(TAG, "Failed to set active focus", e);
103        }
104
105        try {
106            boolean ownsFocus = mCarAppFocusManager.isOwningFocus(focusListener,
107                    CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION);
108            Log.d(TAG, "Owns APP_FOCUS_TYPE_NAVIGATION: " + ownsFocus);
109            if (!ownsFocus) {
110                throw new RuntimeException("Focus was not acquired.");
111            }
112        } catch (CarNotConnectedException e) {
113            Log.e(TAG, "Failed to get owned focus", e);
114        }
115
116        try {
117            mCarNavigationStatusManager
118                    .sendNavigationStatus(CarNavigationStatusManager.STATUS_ACTIVE);
119        } catch (CarNotConnectedException e) {
120            Log.e(TAG, "Failed to set navigation status", e);
121        }
122    }
123}
124