<commit_before>/*
 * Copyright 2014 Google Inc.
 *
 * 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.google.android.apps.muzei.widget

import android.app.PendingIntent
import android.appwidget.AppWidgetManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.os.Build
import android.support.annotation.LayoutRes
import android.support.annotation.RequiresApi
import android.util.Log
import android.util.TypedValue
import android.view.View
import android.widget.RemoteViews
import androidx.core.os.bundleOf
import com.google.android.apps.muzei.render.BitmapRegionLoader
import com.google.android.apps.muzei.room.Artwork
import com.google.android.apps.muzei.room.MuzeiDatabase
import com.google.android.apps.muzei.room.Provider
import com.google.android.apps.muzei.sources.allowsNextArtwork
import com.google.android.apps.muzei.wallpaper.WallpaperActiveState
import kotlinx.coroutines.experimental.launch
import net.nurik.roman.muzei.R

private const val TAG = "updateAppWidget"

/**
 * Provide a preview for pinning the widget
 */
@RequiresApi(Build.VERSION_CODES.O)
fun showWidgetPreview(context: Context) = launch {
    val widget = ComponentName(context, MuzeiAppWidgetProvider::class.java)
    val appWidgetManager = AppWidgetManager.getInstance(context)
    if (!appWidgetManager.isRequestPinAppWidgetSupported) {
        // No preview to show
        return@launch
    }
    val provider = MuzeiDatabase.getInstance(context).providerDao().getCurrentProvider()
    val artwork = MuzeiDatabase.getInstance(context).artworkDao().getCurrentArtwork()
    if (provider == null || artwork == null) {
        Log.w(TAG, "No current artwork found")
        return@launch
    }
    val remoteViews = createRemoteViews(context, provider, artwork,
            context.resources.getDimensionPixelSize(R.dimen.widget_min_width),
            context.resources.getDimensionPixelSize(R.dimen.widget_min_height))
            ?: return@launch
    val extras = bundleOf(AppWidgetManager.EXTRA_APPWIDGET_PREVIEW to remoteViews)
    try {
        appWidgetManager.requestPinAppWidget(widget, extras, null)
    } catch (ignored: IllegalStateException) {
        // The user exited out of the app before we could pop up the pin widget dialog
    }
}

/**
 * Async operation used to update the widget or provide a preview for pinning the widget.
 */
suspend fun updateAppWidget(context: Context) {
    val widget = ComponentName(context, MuzeiAppWidgetProvider::class.java)
    val appWidgetManager = AppWidgetManager.getInstance(context)
    val appWidgetIds = appWidgetManager.getAppWidgetIds(widget)
    if (appWidgetIds.isEmpty()) {<commit_msg>Skip updating the widget if AppWidgetManager.getInstance is null<commit_after>/*
 * Copyright 2014 Google Inc.
 *
 * 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.google.android.apps.muzei.widget

import android.app.PendingIntent
import android.appwidget.AppWidgetManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.os.Build
import android.support.annotation.LayoutRes
import android.support.annotation.RequiresApi
import android.util.Log
import android.util.TypedValue
import android.view.View
import android.widget.RemoteViews
import androidx.core.os.bundleOf
import com.google.android.apps.muzei.render.BitmapRegionLoader
import com.google.android.apps.muzei.room.Artwork
import com.google.android.apps.muzei.room.MuzeiDatabase
import com.google.android.apps.muzei.room.Provider
import com.google.android.apps.muzei.sources.allowsNextArtwork
import com.google.android.apps.muzei.wallpaper.WallpaperActiveState
import kotlinx.coroutines.experimental.launch
import net.nurik.roman.muzei.R

private const val TAG = "updateAppWidget"

/**
 * Provide a preview for pinning the widget
 */
@RequiresApi(Build.VERSION_CODES.O)
fun showWidgetPreview(context: Context) = launch {
    val widget = ComponentName(context, MuzeiAppWidgetProvider::class.java)
    val appWidgetManager = AppWidgetManager.getInstance(context)
    if (!appWidgetManager.isRequestPinAppWidgetSupported) {
        // No preview to show
        return@launch
    }
    val provider = MuzeiDatabase.getInstance(context).providerDao().getCurrentProvider()
    val artwork = MuzeiDatabase.getInstance(context).artworkDao().getCurrentArtwork()
    if (provider == null || artwork == null) {
        Log.w(TAG, "No current artwork found")
        return@launch
    }
    val remoteViews = createRemoteViews(context, provider, artwork,
            context.resources.getDimensionPixelSize(R.dimen.widget_min_width),
            context.resources.getDimensionPixelSize(R.dimen.widget_min_height))
            ?: return@launch
    val extras = bundleOf(AppWidgetManager.EXTRA_APPWIDGET_PREVIEW to remoteViews)
    try {
        appWidgetManager.requestPinAppWidget(widget, extras, null)
    } catch (ignored: IllegalStateException) {
        // The user exited out of the app before we could pop up the pin widget dialog
    }
}

/**
 * Async operation used to update the widget or provide a preview for pinning the widget.
 */
suspend fun updateAppWidget(context: Context) {
    val widget = ComponentName(context, MuzeiAppWidgetProvider::class.java)
    val appWidgetManager = AppWidgetManager.getInstance(context) ?: return
    val appWidgetIds = appWidgetManager.getAppWidgetIds(widget)
    if (appWidgetIds.isEmpty()) {
{'fraction_non_alphanumeric': 0.08382959230416857, 'fraction_numerical': 0.0024431210871888836, 'mean_word_length': 4.408753096614368, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '536507', 'n_tokens_mistral': 1801, 'n_tokens_neox': 1786, 'n_words': 496}