Search in sources :

Example 1 with LayoutRes

use of android.support.annotation.LayoutRes in project muzei by romannurik.

the class AppWidgetUpdateTask method doInBackground.

@Override
protected Boolean doInBackground(Void... params) {
    ComponentName widget = new ComponentName(mContext, MuzeiAppWidgetProvider.class);
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
    int[] appWidgetIds = appWidgetManager.getAppWidgetIds(widget);
    if (appWidgetIds.length == 0) {
        // No app widgets, nothing to do
        Log.i(TAG, "No AppWidgets found");
        return true;
    }
    String[] projection = new String[] { BaseColumns._ID, MuzeiContract.Artwork.COLUMN_NAME_TITLE, MuzeiContract.Artwork.COLUMN_NAME_BYLINE, MuzeiContract.Sources.COLUMN_NAME_SUPPORTS_NEXT_ARTWORK_COMMAND };
    ContentResolver contentResolver = mContext.getContentResolver();
    Cursor artwork = contentResolver.query(MuzeiContract.Artwork.CONTENT_URI, projection, null, null, null);
    if (artwork == null || !artwork.moveToFirst()) {
        Log.w(TAG, "No current artwork found");
        if (artwork != null) {
            artwork.close();
        }
        return false;
    }
    String title = artwork.getString(artwork.getColumnIndex(MuzeiContract.Artwork.COLUMN_NAME_TITLE));
    String byline = artwork.getString(artwork.getColumnIndex(MuzeiContract.Artwork.COLUMN_NAME_BYLINE));
    String contentDescription = !TextUtils.isEmpty(title) ? title : byline;
    Uri imageUri = ContentUris.withAppendedId(MuzeiContract.Artwork.CONTENT_URI, artwork.getLong(artwork.getColumnIndex(BaseColumns._ID)));
    boolean supportsNextArtwork = artwork.getInt(artwork.getColumnIndex(MuzeiContract.Sources.COLUMN_NAME_SUPPORTS_NEXT_ARTWORK_COMMAND)) != 0;
    artwork.close();
    // Update the widget(s) with the new artwork information
    PackageManager packageManager = mContext.getPackageManager();
    Intent launchIntent = packageManager.getLaunchIntentForPackage(mContext.getPackageName());
    PendingIntent launchPendingIntent = PendingIntent.getActivity(mContext, 0, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    Intent nextArtworkIntent = new Intent(mContext, MuzeiAppWidgetProvider.class);
    nextArtworkIntent.setAction(MuzeiAppWidgetProvider.ACTION_NEXT_ARTWORK);
    PendingIntent nextArtworkPendingIntent = PendingIntent.getBroadcast(mContext, 0, nextArtworkIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics();
    int smallWidgetHeight = mContext.getResources().getDimensionPixelSize(R.dimen.widget_small_height_breakpoint);
    for (int widgetId : appWidgetIds) {
        Bundle extras = appWidgetManager.getAppWidgetOptions(widgetId);
        int widgetWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, extras.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH), displayMetrics);
        int widgetHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, extras.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT), displayMetrics);
        Bitmap image;
        try {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(contentResolver.openInputStream(imageUri), null, options);
            int width = options.outWidth;
            int height = options.outHeight;
            options.inJustDecodeBounds = false;
            options.inSampleSize = Math.min(ImageUtil.calculateSampleSize(width, widgetWidth), ImageUtil.calculateSampleSize(height, widgetHeight));
            image = BitmapFactory.decodeStream(contentResolver.openInputStream(imageUri), null, options);
        } catch (FileNotFoundException e) {
            Log.e(TAG, "Could not find current artwork image", e);
            return false;
        }
        // Even after using sample size to scale an image down, it might be larger than the
        // maximum bitmap memory usage for widgets
        Bitmap scaledImage = scaleBitmap(image, widgetWidth, widgetHeight);
        @LayoutRes int widgetLayout = widgetHeight < smallWidgetHeight ? R.layout.widget_small : R.layout.widget;
        RemoteViews remoteViews = new RemoteViews(mContext.getPackageName(), widgetLayout);
        remoteViews.setContentDescription(R.id.widget_background, contentDescription);
        remoteViews.setImageViewBitmap(R.id.widget_background, scaledImage);
        remoteViews.setOnClickPendingIntent(R.id.widget_background, launchPendingIntent);
        remoteViews.setOnClickPendingIntent(R.id.widget_next_artwork, nextArtworkPendingIntent);
        if (supportsNextArtwork) {
            remoteViews.setViewVisibility(R.id.widget_next_artwork, View.VISIBLE);
        } else {
            remoteViews.setViewVisibility(R.id.widget_next_artwork, View.GONE);
        }
        appWidgetManager.updateAppWidget(widgetId, remoteViews);
    }
    return true;
}
Also used : Bundle(android.os.Bundle) AppWidgetManager(android.appwidget.AppWidgetManager) FileNotFoundException(java.io.FileNotFoundException) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) Cursor(android.database.Cursor) Uri(android.net.Uri) DisplayMetrics(android.util.DisplayMetrics) ContentResolver(android.content.ContentResolver) Bitmap(android.graphics.Bitmap) RemoteViews(android.widget.RemoteViews) LayoutRes(android.support.annotation.LayoutRes) PackageManager(android.content.pm.PackageManager) ComponentName(android.content.ComponentName) PendingIntent(android.app.PendingIntent) BitmapFactory(android.graphics.BitmapFactory)

Example 2 with LayoutRes

use of android.support.annotation.LayoutRes in project epoxy by airbnb.

the class EpoxyProcessor method addDefaultLayoutMethodIfNeeded.

/**
   * If there is no existing implementation of getDefaultLayout we can generate an implementation.
   * This relies on a layout res being set in the @EpoxyModelClass annotation.
   */
private void addDefaultLayoutMethodIfNeeded(TypeElement originalClassElement, List<MethodSpec> methods) {
    MethodSpec getDefaultLayoutMethod = MethodSpec.methodBuilder(GET_DEFAULT_LAYOUT_METHOD_NAME).addAnnotation(Override.class).addAnnotation(LayoutRes.class).addModifiers(Modifier.PROTECTED).returns(TypeName.INT).build();
    if (implementsMethod(originalClassElement, getDefaultLayoutMethod, typeUtils)) {
        return;
    }
    EpoxyModelClass annotation = findClassAnnotationWithLayout(originalClassElement);
    if (annotation == null) {
        logError("Model must use %s annotation if it does not implement %s. (class: %s)", EpoxyModelClass.class, GET_DEFAULT_LAYOUT_METHOD_NAME, originalClassElement.getSimpleName());
        return;
    }
    int layoutRes;
    try {
        layoutRes = annotation.layout();
    } catch (AnnotationTypeMismatchException e) {
        logError("Invalid layout value in %s annotation. (class: %s). %s: %s", EpoxyModelClass.class, originalClassElement.getSimpleName(), e.getClass().getSimpleName(), e.getMessage());
        return;
    }
    if (layoutRes == 0) {
        logError("Model must specify a valid layout resource in the %s annotation. (class: %s)", EpoxyModelClass.class, originalClassElement.getSimpleName());
        return;
    }
    AndroidResource layoutResource = resourceProcessor.getResourceForValue(layoutRes);
    getDefaultLayoutMethod = getDefaultLayoutMethod.toBuilder().addStatement("return $L", layoutResource.code).build();
    methods.add(getDefaultLayoutMethod);
}
Also used : LayoutRes(android.support.annotation.LayoutRes) MethodSpec(com.squareup.javapoet.MethodSpec) AnnotationTypeMismatchException(java.lang.annotation.AnnotationTypeMismatchException)

Example 3 with LayoutRes

use of android.support.annotation.LayoutRes in project StarWars.Android by Yalantis.

the class ProfileAdapter method onCreateViewHolder.

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    @LayoutRes int layoutRes = getLayoutId(viewType);
    View itemView = LayoutInflater.from(parent.getContext()).inflate(layoutRes, parent, false);
    return new ViewHolder(itemView);
}
Also used : LayoutRes(android.support.annotation.LayoutRes) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) View(android.view.View)

Example 4 with LayoutRes

use of android.support.annotation.LayoutRes in project flow by square.

the class BasicDispatcher method dispatch.

@Override
public void dispatch(@NonNull Traversal traversal, @NonNull TraversalCallback callback) {
    Log.d("BasicDispatcher", "dispatching " + traversal);
    Object destKey = traversal.destination.top();
    ViewGroup frame = (ViewGroup) activity.findViewById(R.id.basic_activity_frame);
    // We're already showing something, clean it up.
    if (frame.getChildCount() > 0) {
        final View currentView = frame.getChildAt(0);
        // Save the outgoing view state.
        if (traversal.origin != null) {
            traversal.getState(traversal.origin.top()).save(currentView);
        }
        // Short circuit if we would just be showing the same view again.
        final Object currentKey = Flow.getKey(currentView);
        if (destKey.equals(currentKey)) {
            callback.onTraversalCompleted();
            return;
        }
        frame.removeAllViews();
    }
    @LayoutRes final int layout;
    if (destKey instanceof HelloScreen) {
        layout = R.layout.hello_screen;
    } else if (destKey instanceof WelcomeScreen) {
        layout = R.layout.welcome_screen;
    } else {
        throw new AssertionError("Unrecognized screen " + destKey);
    }
    View incomingView = //
    LayoutInflater.from(traversal.createContext(destKey, activity)).inflate(layout, frame, false);
    frame.addView(incomingView);
    traversal.getState(traversal.destination.top()).restore(incomingView);
    callback.onTraversalCompleted();
}
Also used : LayoutRes(android.support.annotation.LayoutRes) ViewGroup(android.view.ViewGroup) View(android.view.View)

Aggregations

LayoutRes (android.support.annotation.LayoutRes)4 View (android.view.View)2 PendingIntent (android.app.PendingIntent)1 AppWidgetManager (android.appwidget.AppWidgetManager)1 ComponentName (android.content.ComponentName)1 ContentResolver (android.content.ContentResolver)1 Intent (android.content.Intent)1 PackageManager (android.content.pm.PackageManager)1 Cursor (android.database.Cursor)1 Bitmap (android.graphics.Bitmap)1 BitmapFactory (android.graphics.BitmapFactory)1 Uri (android.net.Uri)1 Bundle (android.os.Bundle)1 RecyclerView (android.support.v7.widget.RecyclerView)1 DisplayMetrics (android.util.DisplayMetrics)1 ViewGroup (android.view.ViewGroup)1 RemoteViews (android.widget.RemoteViews)1 TextView (android.widget.TextView)1 MethodSpec (com.squareup.javapoet.MethodSpec)1 FileNotFoundException (java.io.FileNotFoundException)1