Home | History | Annotate | Download | only in sample
      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  */
     16 package com.android.car.cluster.sample;
     17 
     18 import android.content.Context;
     19 import android.content.pm.ApplicationInfo;
     20 import android.content.pm.PackageManager;
     21 import android.content.res.Resources;
     22 import android.content.res.TypedArray;
     23 import android.util.Log;
     24 
     25 /**
     26  * An immutable class which hold the the information about the currently connected media app.
     27  */
     28 public class MediaAppInfo {
     29     private static final String TAG = DebugUtil.getTag(MediaAppInfo.class);
     30 
     31     private int mAccentColor;
     32 
     33     public MediaAppInfo(Context context, String packageName) {
     34         try {
     35             PackageManager packageManager = context.getPackageManager();
     36             ApplicationInfo appInfo = packageManager.getApplicationInfo(packageName,
     37                     PackageManager.GET_META_DATA);
     38 
     39             fetchAppColors(packageName, appInfo.theme, context);
     40         } catch (PackageManager.NameNotFoundException e) {
     41             Log.e(TAG, "Got a component that doesn't exist (" + packageName + ")");
     42         }
     43     }
     44 
     45     public int getMediaClientAccentColor() {
     46         return mAccentColor;
     47     }
     48 
     49     private void fetchAppColors(String packageName, int appTheme, Context context) {
     50         TypedArray ta = null;
     51         try {
     52             Context packageContext = context.createPackageContext(packageName, 0);
     53             packageContext.setTheme(appTheme);
     54             Resources.Theme theme = packageContext.getTheme();
     55             ta = theme.obtainStyledAttributes(new int[]{ android.R.attr.colorAccent });
     56             mAccentColor = ta.getColor(1,
     57                     context.getResources().getColor(android.R.color.holo_green_light));
     58         } catch (PackageManager.NameNotFoundException e) {
     59             Log.e(TAG, "Unable to update media client package attributes.", e);
     60         } finally {
     61             if (ta != null) {
     62                 ta.recycle();
     63             }
     64         }
     65     }
     66 }