1 // Copyright 2018 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package org.chromium.chromecast.shell; 6 7 import android.os.Build; 8 9 import com.google.android.things.AndroidThings; 10 import com.google.android.things.factory.FactoryDataManager; 11 import com.google.android.things.update.UpdateManager; 12 13 import org.chromium.base.Log; 14 import org.chromium.base.annotations.CalledByNative; 15 import org.chromium.base.annotations.JNINamespace; 16 17 import java.io.BufferedReader; 18 import java.io.IOException; 19 import java.io.InputStream; 20 import java.io.InputStreamReader; 21 import java.util.ArrayList; 22 23 /** 24 * Java implementation of CastSysInfoAndroidThings methods. 25 */ 26 @JNINamespace("chromecast") 27 public final class CastSysInfoAndroidThings { 28 private static final String TAG = CastSysInfoAndroidThings.class.getSimpleName(); 29 private static final String FACTORY_LOCALE_LIST_FILE = "locale_list.txt"; 30 31 @CalledByNative getProductName()32 private static String getProductName() { 33 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 34 return AndroidThings.Product.NAME; 35 } else { 36 return Build.PRODUCT; 37 } 38 } 39 40 @CalledByNative getDeviceModel()41 private static String getDeviceModel() { 42 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 43 return AndroidThings.Product.MODEL; 44 } else { 45 return Build.MODEL; 46 } 47 } 48 49 @CalledByNative getManufacturer()50 private static String getManufacturer() { 51 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 52 return AndroidThings.Product.MANUFACTURER; 53 } else { 54 return Build.MANUFACTURER; 55 } 56 } 57 58 @CalledByNative getReleaseChannel()59 private static String getReleaseChannel() { 60 return UpdateManager.getInstance().getChannel(); 61 } 62 63 @CalledByNative getFactoryLocaleList()64 private static String[] getFactoryLocaleList() { 65 ArrayList<String> locale_list = new ArrayList<String>(); 66 try { 67 FactoryDataManager factoryManager = FactoryDataManager.getInstance(); 68 try (InputStream input = factoryManager.openFile(FACTORY_LOCALE_LIST_FILE); 69 BufferedReader reader = new BufferedReader(new InputStreamReader(input))) { 70 for (String line = reader.readLine(); line != null; line = reader.readLine()) { 71 locale_list.add(line); 72 } 73 } 74 } catch (IllegalStateException e) { 75 Log.e(TAG, "Failed to connect to FactoryDataService", e); 76 } catch (IllegalArgumentException | IOException e) { 77 Log.w(TAG, "Factory file %s doesn't exist or can't be opened.", 78 FACTORY_LOCALE_LIST_FILE, e); 79 } catch (java.lang.SecurityException e) { 80 // Thrown when com.google.android.things.permission.READ_FACTORY_DATA is missing. 81 Log.e(TAG, "Failed to read factory data.", e); 82 } 83 return locale_list.toArray(new String[locale_list.size()]); 84 } 85 } 86