<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Saurabh Arora</title>
    <description>The latest articles on DEV Community by Saurabh Arora (@saurabh_arora90).</description>
    <link>https://dev.to/saurabh_arora90</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F195011%2F715b3627-ba44-4d9c-9ded-72bc480da37d.jpg</url>
      <title>DEV Community: Saurabh Arora</title>
      <link>https://dev.to/saurabh_arora90</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/saurabh_arora90"/>
    <language>en</language>
    <item>
      <title>Preparing your app for Android Q</title>
      <dc:creator>Saurabh Arora</dc:creator>
      <pubDate>Sun, 14 Jul 2019 05:11:43 +0000</pubDate>
      <link>https://dev.to/saurabh_arora90/preparing-your-app-for-android-q-2gk4</link>
      <guid>https://dev.to/saurabh_arora90/preparing-your-app-for-android-q-2gk4</guid>
      <description>&lt;p&gt;We are into the 10th year of Android development (Android Q should be 10.0). As per Beta 4, Android Q is officially API level 29. Even though we have a Beta 5 and a Beta 6 is expected, the API's have been marked as final and now is a good time to see how Android Q will affect your app and what changes should be made to fully support Android Q.&lt;/p&gt;

&lt;p&gt;The important changes (and not the full list) introduced in Android Q can be divided into two categories: a) Privacy &amp;amp; Security. b) User Experience&lt;/p&gt;




&lt;h2&gt;
  
  
  1) Privacy &amp;amp; Security
&lt;/h2&gt;

&lt;h4&gt;
  
  
  a) Background Activity Starts
&lt;/h4&gt;

&lt;p&gt;If you try to start an activity, while your app is in the background, it will no longer be allowed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Affects:&lt;/strong&gt; All apps running on Q (irrespective of target SDK). An exception is thrown if the app targets Android Q while the activity just doesn't launch if the app doesn't target Android Q but is running on an Android Q device.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Exceptions:&lt;/strong&gt; Bound services such as accessibility, auto-fill, etc. If the app receives a &lt;code&gt;PendingIntent&lt;/code&gt; from the system, we use can use it to launch an activity. If the app has &lt;code&gt;SYSTEM_ALERT_WINDOW&lt;/code&gt; permission (removed in the Android GO edition) or the app called &lt;code&gt;finish()&lt;/code&gt; on an activity very recently (would not recommend relying on this. The idea of "recently" can be very dubious), then your app is exempted from this restriction as well.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Recommended approach:&lt;/strong&gt; Notification Triggered activity&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;So if we want to launch an activity from the background, first create a Notification to display to the user. In that notification, add a Fullscreen pending intent. Also, add the &lt;code&gt;USE_FULL_SCREEN_INTENT&lt;/code&gt; permission to your manifest. Now, when the notification is triggered, the system will launch the full-screen intent for you.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gotcha's:&lt;/strong&gt; System decides between when to show a notification and when to show the activity. If the user is actively using the device, then a heads up notification is displayed. If the device is ideal or when the user interacts with the notification, the full-screen activity is launched. Think of receiving a phone call (heads up notification while using the phone, otherwise full-screen activity).&lt;/p&gt;

&lt;h4&gt;
  
  
  b) Hardware Identifiers
&lt;/h4&gt;

&lt;p&gt;Access to non-resettable device identifiers has been revoked in Android Q.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Affects:&lt;/strong&gt; All apps running on Q (irrespective of target SDK). An exception is thrown if Target SDK is Q and a null is returned if Target SDK &amp;lt; Q&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Avoid:&lt;/strong&gt; Mac Address is now randomized, while IMEI (&lt;code&gt;TelephonyManager.getDeviceId()&lt;/code&gt;) and serial number are no longer accessible. They are now bucketed under "privileged permissions" and are only available for carrier apps.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Recommended approach:&lt;/strong&gt; Use resettable identifiers such as Advertising ID, Instance ID or Globally-unique IDs (GUIDs). See &lt;a href="https://developer.android.com/training/articles/user-data-ids" rel="noopener noreferrer"&gt;Best practices for unique identifiers&lt;/a&gt; for more details on which identifier to use for which scenario.&lt;/p&gt;

&lt;h4&gt;
  
  
  c) Background Location
&lt;/h4&gt;

&lt;p&gt;Beginning with Android Q, the system will distinguish between location requests made in the foreground vs background.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TF3TIXRn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/676/0%2A7IOuJFOmHcRJfSPc" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TF3TIXRn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/676/0%2A7IOuJFOmHcRJfSPc" alt="The location permission request will now have 3 options: Allow all the time?. Allow only when using the app (just foreground access) and Deny (No access)" width="338" height="689"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The location permission request will now have 3 options: Allow all the time?. Allow only when using the app (just foreground access) and Deny (No access)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Affects:&lt;/strong&gt; Depends. If the app targets Q then you need to request for a new background location permission. If the app doesn't target Q, then it will automatically get this permission if it already had location access permissions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zbDmuAVY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/1400/0%2Ar0C-ormXfsTiqA73" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zbDmuAVY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/1400/0%2Ar0C-ormXfsTiqA73" width="800" height="289"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Recommended approach:&lt;/strong&gt; If the app needs one-time access to the user's location to complete some tasks, use a foreground service with the &lt;a href="https://developer.android.com/reference/android/R.attr.html#foregroundServiceType" rel="noopener noreferrer"&gt;foregroundServiceType&lt;/a&gt; set as &lt;code&gt;location&lt;/code&gt; in the app's manifest file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;service android:name="MyNavigationService"
android:foregroundServiceType="location" ... /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;If the app needs access to the device location all the time such as for geofencing, then it can set up a request for background location permission. Other aspects of the app (such as how the location is fetched and used) don't need to change. To request access for background location, add the &lt;code&gt;ACCESS_BACKGROUND_LOCATION&lt;/code&gt; permission to the manifest:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Gotcha's:&lt;/strong&gt; &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---TUm0g8H--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/676/0%2A36Cz5oh0AMROBWXF" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---TUm0g8H--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/676/0%2A36Cz5oh0AMROBWXF" alt="Reminder triggered by the system about app accessing location in the background" width="338" height="689"&gt;&lt;/a&gt;&lt;br&gt;
A couple of important things to be aware off: The user might receive a reminder after giving an app accesses device location in the background and just like any other permission, the user can revoke the background location permission that was granted to the app. This is especially critical for apps not targeting Q but running on Android Q devices since it would get the background permission by default if it had the location permission. Make sure the app gracefully handles such scenarios. For this reason, whenever the app starts a service or requests for location, check whether the user still allows the app to access location information.&lt;/p&gt;


&lt;h2&gt;
  
  
  2) User Experiences
&lt;/h2&gt;
&lt;h4&gt;
  
  
  a) Gesture Navigation
&lt;/h4&gt;

&lt;p&gt;In Android Q, Google has tried to unify all the gesture navigations out there with a new gesture navigation system to replace the floating system navigation bar (back, home, and recents)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FgAaOYsV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/1400/0%2A7jJmcpK3ZUdL5_Qr" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FgAaOYsV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/1400/0%2A7jJmcpK3ZUdL5_Qr" width="762" height="460"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Affects:&lt;/strong&gt; So if your app contains gestures, then they can possibly conflict with the system gestures. This will affect all apps that run on Android Q (irrespective of target SDK) since the system gestures will get the first dibs on handling the gesture.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Recommended approach:&lt;/strong&gt; Since the floating system navigation bar is effectively gone, it is recommended that apps now take full advantage of the new real estate available to them and provide an immersive experience to users.&lt;/p&gt;

&lt;p&gt;To build a more immersive experience, apps should draw behind the status and the navigation bar. First, request the navigation and status bar to be transparent.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;style name="AppTheme"&amp;gt;
......
&amp;lt;item name="android:navigationBarColor"&amp;gt;@android:color/transparent&amp;lt;/item&amp;gt;
&amp;lt;!-- Optional, but recommended for full edge-to-edge rendering --&amp;gt;
    &amp;lt;item name="android:statusBarColor"&amp;gt;@android:color/transparent&amp;lt;/item&amp;gt;
&amp;lt;/style&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Next, request your activity/view to be laid out full screen:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;view.systemUiVisibility =
//Layout as if the navigation bar was hidden
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or
//Layout as if the status bar was hidden
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
or
//Layout at its most extreme
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now that the app has been laid out in full screen and behind the (transparent) system components, it could be that some clickable component of the app overlaps with the system UI. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdyfgkft1mfbwi9ku0jui.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdyfgkft1mfbwi9ku0jui.png" alt="FAB overlapping with the floating navigation bar. Floating Navigation bar enabled for ease of clarity" width="289" height="595"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To resolve this, use Insets.&lt;/p&gt;

&lt;p&gt;Insets are a collection of values that tell us how much to move our views by so that they don't conflict with the system UI. This information is provided by the &lt;a href="https://developer.android.com/reference/android/view/WindowInsets" rel="noopener noreferrer"&gt;&lt;code&gt;WindowInset&lt;/code&gt;&lt;/a&gt; class. Since API Level 20, Android has provided developers with System Window Insets, which tells us how much space is taken up by the system components (such as the floating navigation bar and the status bar). This information can be used to move the clickable views so that they can be clicked instead of the system UI.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;For the FAB, set up a listener to WindowsInsets, and apply the bottom window inset as a bottom margin.&lt;/p&gt;

&lt;p&gt;Similarly, beginning with Android Q, the system exposes gesture navigation insets, to move the swipeable views so that they don't conflict with the system gestures. These are exposed through &lt;a href="https://developer.android.com/reference/android/view/WindowInsets.html#getSystemGestureInsets()" rel="noopener noreferrer"&gt;&lt;code&gt;Insets.getSystemGestureInsets()&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But what if we don't want to move the views and have some content in those gesture zones which might conflict with the app? &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiblntecfxrxlu4yefv24.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiblntecfxrxlu4yefv24.png" alt="System Gesture insets region highlighted in yellow." width="306" height="631"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In those scenarios, the app can define regions in which the app will get to consume the touch event first and not the system gestures. It can do this by passing a &lt;code&gt;List&amp;lt;Rect&amp;gt;&lt;/code&gt; to the &lt;code&gt;View.setSystemGestureExclusionRects()&lt;/code&gt; API introduced in Android Q. This method is also available in &lt;code&gt;ViewCompat&lt;/code&gt; as of &lt;code&gt;androidx.core:core:1.1.0&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gotcha's:&lt;/strong&gt; You can only opt out of the back gesture bounds. Home gesture bounds are reserved and cannot be overridden. This is because there is only one way to exit the app. If your app conflicts with the home gesture, use &lt;code&gt;WindowInsets.getMandatorySystemGestureInsets()&lt;/code&gt; to get the bounds of the mandatory region and shift your views out of the region accordingly.&lt;/p&gt;

&lt;h4&gt;
  
  
  b) Dark Theme
&lt;/h4&gt;

&lt;p&gt;Over the last year or so, we have seen a lot of apps provide support for dark theme in their apps. Android Q brings this toggle to the system setting.&lt;/p&gt;

&lt;p&gt;There are 3 ways a user can enable dark mode:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A new system setting (Settings -&amp;gt; Display -&amp;gt; Theme)&lt;/li&gt;
&lt;li&gt;A new Quick Settings&lt;/li&gt;
&lt;li&gt;Depending on the manufacturer, through Battery Saver Mode.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In order to support the system-wide Dark Theme configuration, your app's default theme should inherit from the DayNight theme. This ties the app's main theme to the system-controlled night mode flags. This works by pulling the resources from the &lt;code&gt;night&lt;/code&gt; qualifier folders.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!--App Compat Style--&amp;gt;
&amp;lt;style name="AppTheme" parent="Theme.AppCompat.DayNight"&amp;gt;
&amp;lt;!--Material Components--&amp;gt;
&amp;lt;style name="AppTheme" parent="Theme.MaterialComponents.DayNight"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you look inside the App Compat styles, you'll notice that &lt;code&gt;DayNight&lt;/code&gt; theme points to &lt;code&gt;AppCompat.Light&lt;/code&gt; in the default configuration and points to &lt;code&gt;AppCompat&lt;/code&gt; in the &lt;code&gt;night&lt;/code&gt; configuration&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!--values/themes.xml--&amp;gt;
&amp;lt;style name="Theme.AppCompat.DayNight" parent="Theme.AppCompat.Light"&amp;gt;
&amp;lt;!--values-night/themes.xml--&amp;gt;
&amp;lt;style name="Theme.AppCompat.DayNight" parent="Theme.AppCompat"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you wish to provide a toggle to the users to control the theme from within the app, you can call &lt;a href="https://developer.android.com/reference/androidx/appcompat/app/AppCompatDelegate#setdefaultnightmode" rel="noopener noreferrer"&gt;&lt;code&gt;AppCompatDelegate.setDefaultNightMode()&lt;/code&gt;&lt;/a&gt;, which takes in one of the four modes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;MODE_NIGHT_YES&lt;/code&gt; - Show dark theme&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;MODE_NIGHT_NO&lt;/code&gt; - Show Light Theme&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;MODE_NIGHT_FOLLOW_SYSTEM&lt;/code&gt; - Follow System Settings&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;MODE_NIGHT_AUTO_BATTERY&lt;/code&gt; - Dark theme when the device goes into power saving mode.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Gotcha's:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;a) Don't hardcode colors. If there are hardcoded colors in layouts files, it's a red flag. Replace them with color references. Even better: use attributes to reference colors.&lt;/p&gt;

&lt;p&gt;b) Check drawables and provide alternate resources for them under the &lt;code&gt;night&lt;/code&gt; qualifier.&lt;/p&gt;

&lt;p&gt;c) For vector drawables, don't have colors hardcoded in them. Use &lt;code&gt;tint&lt;/code&gt; to style vector resources with the correct color.&lt;/p&gt;

&lt;p&gt;d) Make sure all color resources have corresponding alternatives inside the &lt;code&gt;night&lt;/code&gt; folder (values-night/colors.xml)&lt;/p&gt;

&lt;p&gt;e) If the app uses &lt;code&gt;RemoteViews&lt;/code&gt; for notifications or Widgets, remember to test them as well. If the app uses default Notification styles, then it should be good to go since the system will handle them.&lt;/p&gt;

&lt;p&gt;f) Configuration Change for &lt;code&gt;uiMode&lt;/code&gt; gets invoked whenever the device switches between normal and dark mode. The user might do that manually, or it might kick in when the device battery gets low. If the app does not handle configuration changes properly, the UI will lose context when the device switches between normal and dark mode, as the visible activities and their fragments get destroyed and recreated. So, even if the UI is locked to a single orientation, please add configuration change support. Also, if the app is manually managing configuration changes via &lt;code&gt;android:configChanges&lt;/code&gt;, there is a good chance it'll want to manage uiMode manually as well. For example, if the app involves continuous playback (e.g., a video player), it might not want to interrupt playback just because the device switched between normal and dark mode.&lt;/p&gt;




&lt;p&gt;A bunch of other things were also introduced in Android Q which I haven't covered in details such as &lt;code&gt;Settings Panel&lt;/code&gt;, a new &lt;code&gt;AudioPlaybackCapture&lt;/code&gt; API (an API gives apps the ability to copy the audio being played by other apps. This feature is the analog of screen capture, but for audio)&lt;/p&gt;

&lt;p&gt;Lastly, Android Q includes an updated list of restricted non-SDK interfaces whose access while eventual be shut down from the Android SDK. You can find your list of violations in the play console under the Pre Launch Report.&lt;/p&gt;




&lt;p&gt;If you feel there is something important that I missed out, let me know in the comment below.&lt;/p&gt;

</description>
      <category>android</category>
      <category>androiddev</category>
      <category>google</category>
    </item>
  </channel>
</rss>
