title: How to write a use case that retrieves data from Android framework with Context
body: <p>I am migrating an application to MVVM and clean architecture, and I am missing one part of the puzzle.</p>
  <p><strong>The problem domain:</strong></p>
  <p>List all applications on device and display them in the Fragment / Activity</p>
  <p>A device app is represented by its package name:</p>
  <p><code>data class DeviceApp(val packageName: String)</code></p>
  <p>This is how the device apps are listed:</p>
  <pre><code>private fun listAllApplications(context: Context): List&lt;DeviceApp&gt; {
      val ans = mutableListOf&lt;DeviceApp&gt;()
  
      val packageManager: PackageManager = context.applicationContext.packageManager
      val packages = packageManager.getInstalledApplications(PackageManager.GET_META_DATA)
      for (applicationInfo in packages) {
          val packageName = applicationInfo.packageName
          ans.add(DeviceApp(packageName))
      }
  
      return ans
  }
  </code></pre>
  <p>As I understand, calling <code>listAllApplications()</code> should be done in a UseCase inside the 'Domain Layer', which is called by a <code>ViewModel</code>.</p>
  <p>However <code>listAllApplications</code> receives a <code>Context</code>, and the Domain Layer should be plain code only.</p>
  <p>In clean architecture / MVVM, in which layer should I put <code>listAllApplications(context)</code>?</p>
  <p>And more generally, how should the ViewModel interact with Android framework APIs that require <code>Context</code> (location, etc.)?</p>