summaryrefslogtreecommitdiffstats
path: root/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/Image.java
blob: 34bf6b0161330dfac84a3d7080225e0a8263a6ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
 * vim: ts=4 sw=4 expandtab:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.geckoview;

import android.graphics.Bitmap;
import androidx.annotation.AnyThread;
import androidx.annotation.NonNull;
import org.mozilla.gecko.annotation.WrapForJNI;
import org.mozilla.gecko.util.GeckoBundle;
import org.mozilla.gecko.util.ImageResource;

/** Represents an Web API image resource as used in web app manifests and media session metadata. */
@AnyThread
public class Image {
  private final ImageResource.Collection mCollection;

  /* package */ Image(final ImageResource.Collection collection) {
    mCollection = collection;
  }

  /* package */ static Image fromSizeSrcBundle(final GeckoBundle bundle) {
    return new Image(ImageResource.Collection.fromSizeSrcBundle(bundle));
  }

  /**
   * Get the best version of this image for size <code>size</code>. Embedders are encouraged to
   * cache the result of this method keyed with this instance.
   *
   * @param size pixel size at which this image will be displayed at.
   * @return A {@link GeckoResult} that resolves to the bitmap when ready. Will resolve
   *     exceptionally to {@link ImageProcessingException} if the image cannot be processed.
   */
  @NonNull
  public GeckoResult<Bitmap> getBitmap(final int size) {
    return mCollection.getBitmap(size);
  }

  /** Thrown whenever an image cannot be processed by {@link #getBitmap} */
  @WrapForJNI
  public static class ImageProcessingException extends RuntimeException {
    /**
     * Build an instance of this class.
     *
     * @param message description of the error.
     */
    public ImageProcessingException(final String message) {
      super(message);
    }
  }
}