/* * Copyright (C) 2016 The AOKP Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.aokp.romcontrol.util; import android.app.Activity; import android.content.Context; import android.content.pm.ActivityInfo; import android.content.pm.PackageManager; import android.content.res.Configuration; import android.hardware.camera2.CameraAccessException; import android.hardware.camera2.CameraCharacteristics; import android.hardware.camera2.CameraManager; import android.telephony.TelephonyManager; import android.view.Surface; import com.aokp.romcontrol.fragments.ButtonSettingsFragment; public class Utils { /* returns whether the device has volume rocker or not. */ public static boolean hasVolumeRocker(Context context) { final int deviceKeys = context.getResources().getInteger( com.android.internal.R.integer.config_deviceHardwareKeys); return (deviceKeys & ButtonSettingsFragment.ButtonSettingsPreferenceFragment.KEY_MASK_VOLUME) != 0; } /** * Returns whether the device is voice-capable (meaning, it is also a phone). */ public static boolean isVoiceCapable(Context context) { TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); return telephony != null && telephony.isVoiceCapable(); } /** * Locks the activity orientation to the current device orientation * @param activity */ public static void lockCurrentOrientation(Activity activity) { int currentRotation = activity.getWindowManager().getDefaultDisplay().getRotation(); int orientation = activity.getResources().getConfiguration().orientation; int frozenRotation = 0; switch (currentRotation) { case Surface.ROTATION_0: frozenRotation = orientation == Configuration.ORIENTATION_LANDSCAPE ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE : ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; break; case Surface.ROTATION_90: frozenRotation = orientation == Configuration.ORIENTATION_PORTRAIT ? ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT : ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; break; case Surface.ROTATION_180: frozenRotation = orientation == Configuration.ORIENTATION_LANDSCAPE ? ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE : ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; break; case Surface.ROTATION_270: frozenRotation = orientation == Configuration.ORIENTATION_PORTRAIT ? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT : ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; break; } activity.setRequestedOrientation(frozenRotation); } /** * Check if system has a camera. * * @param context * @return */ public static boolean hasCamera(final Context context) { final PackageManager pm = context.getPackageManager(); return pm != null && pm.hasSystemFeature(PackageManager.FEATURE_CAMERA); } /** * Check if system has a front camera. * * @param context * @return */ public static boolean hasFrontCamera(final Context context) { final PackageManager pm = context.getPackageManager(); return pm != null && pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT); } public static boolean deviceSupportsFlashLight(Context context) { CameraManager cameraManager = (CameraManager) context.getSystemService( Context.CAMERA_SERVICE); try { String[] ids = cameraManager.getCameraIdList(); for (String id : ids) { CameraCharacteristics c = cameraManager.getCameraCharacteristics(id); Boolean flashAvailable = c.get(CameraCharacteristics.FLASH_INFO_AVAILABLE); Integer lensFacing = c.get(CameraCharacteristics.LENS_FACING); if (flashAvailable != null && flashAvailable && lensFacing != null && lensFacing == CameraCharacteristics.LENS_FACING_BACK) { return true; } } } catch (CameraAccessException e) { // Ignore } return false; } }