1 package com.android.healthconnect.controller.utils 2 3 import android.content.ActivityNotFoundException 4 import android.content.Context 5 import android.content.Intent 6 import android.net.Uri 7 import android.text.TextUtils 8 import android.util.Log 9 import androidx.fragment.app.FragmentActivity 10 import com.android.healthconnect.controller.R 11 import com.android.healthconnect.controller.permissions.shared.HelpAndFeedbackFragment.Companion.FEEDBACK_INTENT_RESULT_CODE 12 import com.android.healthconnect.controller.permissions.shared.HelpAndFeedbackFragment.Companion.USER_INITIATED_FEEDBACK_BUCKET_ID 13 import com.android.settingslib.HelpUtils 14 import dagger.Module 15 import dagger.Provides 16 import dagger.hilt.EntryPoint 17 import dagger.hilt.InstallIn 18 import dagger.hilt.components.SingletonComponent 19 import javax.inject.Inject 20 21 interface DeviceInfoUtils { isSendFeedbackAvailablenull22 fun isSendFeedbackAvailable(context: Context): Boolean 23 24 fun isPlayStoreAvailable(context: Context): Boolean 25 26 fun openHCGetStartedLink(activity: FragmentActivity) 27 28 fun openSendFeedbackActivity(activity: FragmentActivity) 29 30 fun isIntentHandlerAvailable(context: Context, intent: Intent): Boolean 31 } 32 33 class DeviceInfoUtilsImpl @Inject constructor() : DeviceInfoUtils { 34 35 companion object { 36 private val TAG = "DeviceInfoUtils" 37 } 38 39 override fun isSendFeedbackAvailable(context: Context): Boolean { 40 return isIntentHandlerAvailable(context, Intent(Intent.ACTION_BUG_REPORT)) 41 } 42 43 override fun isPlayStoreAvailable(context: Context): Boolean { 44 val playStorePackageName = context.resources?.getString(R.string.playstore_collection_url) 45 val vendingPackageName = context.resources?.getString(R.string.playstore_package_name) 46 if (TextUtils.isEmpty(playStorePackageName) || playStorePackageName == null) { 47 // Package name not configured. Return. 48 return false 49 } 50 return isIntentHandlerAvailable( 51 context, 52 Intent(Intent.ACTION_VIEW).apply { 53 data = Uri.parse(playStorePackageName) 54 setPackage(vendingPackageName) 55 }) 56 } 57 58 override fun openHCGetStartedLink(activity: FragmentActivity) { 59 val helpUrlString = activity.getString(R.string.hc_get_started_link) 60 val fullUri = HelpUtils.uriWithAddedParameters(activity, Uri.parse(helpUrlString)) 61 val intent = 62 Intent(Intent.ACTION_VIEW, fullUri).apply { 63 flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS 64 } 65 try { 66 activity.startActivity(intent) 67 } catch (e: ActivityNotFoundException) { 68 Log.w(TAG, "Unable to open help center URL.", e) 69 } 70 } 71 72 override fun openSendFeedbackActivity(activity: FragmentActivity) { 73 val intent = Intent(Intent.ACTION_BUG_REPORT) 74 intent.putExtra("category_tag", USER_INITIATED_FEEDBACK_BUCKET_ID) 75 activity.startActivityForResult(intent, FEEDBACK_INTENT_RESULT_CODE) 76 } 77 78 override fun isIntentHandlerAvailable(context: Context, intent: Intent): Boolean { 79 val packageManager = context.packageManager 80 if (intent.resolveActivity(packageManager) != null) { 81 return true 82 } 83 return false 84 } 85 } 86 87 @EntryPoint 88 @InstallIn(SingletonComponent::class) 89 interface DeviceInfoUtilsEntryPoint { deviceInfoUtilsnull90 fun deviceInfoUtils(): DeviceInfoUtils 91 } 92 93 @Module 94 @InstallIn(SingletonComponent::class) 95 class DeviceInfoUtilsModule { 96 @Provides 97 fun providesDeviceInfoUtils(): DeviceInfoUtils { 98 return DeviceInfoUtilsImpl() 99 } 100 } 101