1 /*
2  * Copyright (C) 2018 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.dialer.commandline.impl;
18 
19 import android.content.Context;
20 import android.content.pm.PackageInfo;
21 import android.content.pm.PackageManager.NameNotFoundException;
22 import android.support.annotation.NonNull;
23 import com.android.dialer.commandline.Arguments;
24 import com.android.dialer.commandline.Command;
25 import com.android.dialer.inject.ApplicationContext;
26 import com.google.common.util.concurrent.Futures;
27 import com.google.common.util.concurrent.ListenableFuture;
28 import java.util.Locale;
29 import javax.inject.Inject;
30 
31 /** Print the version name and code. */
32 public class Version implements Command {
33 
34   @NonNull
35   @Override
getShortDescription()36   public String getShortDescription() {
37     return "Print dialer version";
38   }
39 
40   @NonNull
41   @Override
getUsage()42   public String getUsage() {
43     return "version";
44   }
45 
46   private final Context appContext;
47 
48   @Inject
Version(@pplicationContext Context context)49   Version(@ApplicationContext Context context) {
50     this.appContext = context;
51   }
52 
53   @Override
run(Arguments args)54   public ListenableFuture<String> run(Arguments args) throws IllegalCommandLineArgumentException {
55     try {
56       PackageInfo info =
57           appContext.getPackageManager().getPackageInfo(appContext.getPackageName(), 0);
58       return Futures.immediateFuture(
59           String.format(Locale.US, "%s(%d)", info.versionName, info.versionCode));
60     } catch (NameNotFoundException e) {
61       throw new RuntimeException(e);
62     }
63   }
64 }
65