1 /*
2  * Copyright 2020 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.android.car.calendar.common;
18 
19 import android.app.ActivityOptions;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.net.Uri;
23 import android.util.Log;
24 import android.view.Display;
25 import android.widget.Toast;
26 
27 /** Launches a navigation activity. */
28 public class Navigator {
29     private static final String TAG = "CarCalendarNavigator";
30     private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
31 
32     private final Context mContext;
33 
Navigator(Context context)34     public Navigator(Context context) {
35         this.mContext = context;
36     }
37 
38     /** Launches a navigation activity to the given address or place name. */
navigate(String locationText)39     public void navigate(String locationText) {
40         Uri navigateUri = Uri.parse("google.navigation:q=" + locationText);
41         Intent intent = new Intent(Intent.ACTION_VIEW, navigateUri);
42         if (intent.resolveActivity(mContext.getPackageManager()) != null) {
43             if (DEBUG) Log.d(TAG, "Starting navigation");
44 
45             // Workaround to bring GMM to the front. CarLauncher contains an ActivityView that
46             // opens GMM in a virtual display which causes it not to move to the front.
47             // This workaround is not required for other launchers.
48             // TODO(b/153046584): Remove workaround for GMM not moving to front
49             ActivityOptions activityOptions =
50                     ActivityOptions.makeBasic().setLaunchDisplayId(Display.DEFAULT_DISPLAY);
51             mContext.startActivity(intent, activityOptions.toBundle());
52         } else {
53             Toast.makeText(mContext, "Navigation app not found", Toast.LENGTH_LONG).show();
54         }
55     }
56 }
57