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.permissioncontroller.incident;
18 
19 import android.content.Context;
20 import android.content.pm.ApplicationInfo;
21 import android.content.pm.PackageManager;
22 
23 import com.android.permissioncontroller.permission.utils.Utils;
24 
25 import java.text.DateFormat;
26 import java.util.Date;
27 
28 /**
29  * Utility class for formatting the incident report confirmations.
30  */
31 public class Formatting {
32     private final Context mContext;
33     private final PackageManager mPm;
34     private final DateFormat mDateFormat;
35     private final DateFormat mTimeFormat;
36 
37     /**
38      * Constructor.  This object keeps the context.
39      */
Formatting(Context context)40     Formatting(Context context) {
41         mContext = context;
42         mPm = context.getPackageManager();
43         mDateFormat = android.text.format.DateFormat.getDateFormat(context);
44         mTimeFormat = android.text.format.DateFormat.getTimeFormat(context);
45     }
46 
47     /**
48      * Get the name to show the user for an application, given the package name.
49      * If the application can't be found, returns null.
50      */
getAppLabel(String pkg)51     String getAppLabel(String pkg) {
52         ApplicationInfo app;
53         try {
54             app = mPm.getApplicationInfo(pkg, 0);
55         } catch (PackageManager.NameNotFoundException ex) {
56             return null;
57         }
58         return Utils.getAppLabel(app, mContext);
59     }
60 
61     /**
62      * Format the date portion of a {@link System.currentTimeMillis} as a user-visible string.
63      */
getDate(long wallTimeMs)64     String getDate(long wallTimeMs) {
65         return mDateFormat.format(new Date(wallTimeMs));
66     }
67 
68     /**
69      * Format the time portion of a {@link System.currentTimeMillis} as a user-visible string.
70      */
getTime(long wallTimeMs)71     String getTime(long wallTimeMs) {
72         return mTimeFormat.format(new Date(wallTimeMs));
73     }
74 }
75