Home | History | Annotate | Download | only in cluster
      1 /*
      2  * Copyright (C) 2017 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.google.android.car.kitchensink.cluster;
     18 
     19 import android.app.Activity;
     20 import android.car.cluster.CarInstrumentClusterManager;
     21 import android.car.cluster.ClusterActivityState;
     22 import android.graphics.Rect;
     23 import android.os.Bundle;
     24 import android.support.car.Car;
     25 import android.support.car.CarConnectionCallback;
     26 import android.support.car.CarNotConnectedException;
     27 import android.util.Log;
     28 import android.widget.ImageView;
     29 import android.widget.RelativeLayout;
     30 
     31 import com.google.android.car.kitchensink.R;
     32 
     33 /**
     34  * Fake navigation activity for instrument cluster.
     35  */
     36 public class FakeClusterNavigationActivity
     37         extends Activity
     38         implements CarInstrumentClusterManager.Callback {
     39 
     40     private final static String TAG = FakeClusterNavigationActivity.class.getSimpleName();
     41 
     42     private Car mCarApi;
     43     private CarInstrumentClusterManager mClusterManager;
     44     private ImageView mUnobscuredArea;
     45 
     46     @Override
     47     public void onCreate(Bundle savedInstanceState) {
     48         super.onCreate(savedInstanceState);
     49         Log.i(TAG, "onCreate");
     50         setContentView(R.layout.fake_cluster_navigation_activity);
     51         mUnobscuredArea = findViewById(R.id.unobscuredArea);
     52 
     53         mCarApi = Car.createCar(this /* context */, new CarConnectionCallback() {
     54 
     55             @Override
     56             public void onConnected(Car car) {
     57                 onCarConnected(car);
     58             }
     59 
     60             @Override
     61             public void onDisconnected(Car car) {
     62                 onCarDisconnected(car);
     63             }
     64         });
     65         Log.i(TAG, "Connecting to car api...");
     66         mCarApi.connect();
     67     }
     68 
     69 
     70     @Override
     71     public void onClusterActivityStateChanged(String category, Bundle clusterActivityState) {
     72         ClusterActivityState state = ClusterActivityState.fromBundle(clusterActivityState);
     73         Log.i(TAG, "onClusterActivityStateChanged, category: " + category + ", state: " + state);
     74 
     75         Rect unobscured = state.getUnobscuredBounds();
     76         RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
     77                 unobscured.width(), unobscured.height());
     78         lp.setMargins(unobscured.left, unobscured.top, 0, 0);
     79         mUnobscuredArea.setLayoutParams(lp);
     80     }
     81 
     82     private void onCarConnected(Car car) {
     83         Log.i(TAG, "onCarConnected, car: " + car);
     84         try {
     85             mClusterManager = (CarInstrumentClusterManager) car.getCarManager(
     86                     android.car.Car.CAR_INSTRUMENT_CLUSTER_SERVICE);
     87         } catch (CarNotConnectedException e) {
     88             throw new IllegalStateException(e);
     89         }
     90 
     91         try {
     92             Log.i(TAG, "registering callback...");
     93             mClusterManager.registerCallback(CarInstrumentClusterManager.CATEGORY_NAVIGATION, this);
     94             Log.i(TAG, "callback registered");
     95         } catch (android.car.CarNotConnectedException e) {
     96             throw new IllegalStateException(e);
     97         }
     98     }
     99 
    100     private void onCarDisconnected(Car car) {
    101 
    102     }
    103 }