<commit_before>package nz.gen.geek_central.view_cache_demo;
/*
    Demonstration of how to do smooth scrolling of a complex
    image by careful caching of bitmaps--actual view caching.

    Copyright 2011 by <NAME> <<EMAIL>>.

    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.
*/

import android.graphics.PointF;
import android.graphics.RectF;
import android.graphics.Bitmap;
import android.view.MotionEvent;

public class DrawView extends android.view.View
  {
    protected android.content.Context Context;
    protected float ZoomFactor;
    protected float ScrollX, ScrollY; /* [0.0 .. 1.0] */
    protected final float MaxZoomFactor = 32.0f;
    protected final float MinZoomFactor = 1.0f;

    protected Drawer DrawWhat;
    protected boolean UseCaching = true;

    protected static class ViewCacheBits
      {
        public final Bitmap Bits;
        public final RectF Bounds;

        public ViewCacheBits
          (
            Bitmap Bits,
            RectF Bounds
          )
          {
            this.Bits = Bits;
            this.Bounds = Bounds;
          } /*ViewCacheBits*/

      } /*ViewCacheBits*/

    protected PointF
        LastMouse1 = null,
        LastMouse2 = null;
    protected int
        Mouse1ID = -1,
        Mouse2ID = -1;
    protected final float MaxCacheFactor = 2.0f;
      /* how far to cache beyond visible bounds */
    protected ViewCacheBits ViewCache = null;
    protected ViewCacheBuilder BuildViewCache = null;
    protected boolean CacheRebuildNeeded = false;
    boolean MouseMoved = false;

    protected class ViewCacheBuilder extends android.os.AsyncTask<Void, Integer, ViewCacheBits>
      {
        protected RectF ScaledViewBounds, CacheBounds;

        protected void onPreExecute()
          {
            final ViewParms v = new ViewParms();
            ScaledViewBounds = new RectF(0, 0, v.ScaledViewWidth, v.ScaledViewHeight);
            final PointF ViewOffset = ScrollOffset(v);
            final PointF ViewCenter = new PointF
              (
                /*x =*/ v.ViewWidth / 2.0f - ViewOffset.x,
                /*y =*/ v.ViewHeight / 2.0f - ViewOffset.y
              );
            CacheBounds = new RectF
              (
                /*left =*/
                    Math.max
                      (
                        ViewCenter.x - v.ViewWidth * MaxCacheFactor / 2.0f,
                        ScaledViewBounds.left
                      ),
                /*top =*/
                    Math.max
                      (
                        ViewCenter.y - v.ViewHeight * MaxCacheFactor / 2.0f,
                        ScaledViewBounds.top
                      ),
                /*right =*/
                    Math.min
                      (
                        ViewCenter.x + v.ViewWidth * MaxCacheFactor / 2.0f,
                        ScaledViewBounds.right
                      ),
                /*bottom =*/
                    Math.min
                      (
                        ViewCenter.y + v.ViewHeight * MaxCacheFactor / 2.0f,
                        ScaledViewBounds.bottom
                      )
              );
            if (CacheBounds.isEmpty())
              {
              /* can seem to happen, e.g. on orientation change */
                cancel(true);
              } /*if*/
          } /*onPreExecute*/

        protected ViewCacheBits doInBackground
          (
            Void... Unused
          )
          {
            final Bitmap CacheBits =
                Bitmap.createBitmap
                  (
                    /*width =*/ (int)(CacheBounds.right - CacheBounds.left),
                    /*height =*/ (int)(CacheBounds.bottom - CacheBounds.top),
                    /*config =*/ Bitmap.Config.ARGB_8888
                  );
            final android.graphics.Canvas CacheDraw = new android.graphics.Canvas(CacheBits);
            final RectF DestRect = new RectF(ScaledViewBounds);
            DestRect.offset(- CacheBounds.left, - CacheBounds.top);
            DrawWhat.Draw(CacheDraw, DestRect); /* this is the time-consuming part */
            CacheBits.prepareToDraw();
            return
                new ViewCacheBits(CacheBits, CacheBounds);
          } /*doInBackground*/

        protected void onCancelled
          (
            ViewCacheBits Result
          )
          {
            Result.Bits.recycle();
          } /*onCancelled*/

        protected void onPostExecute
          (
            ViewCacheBits Result
          )
          {
            DisposeViewCache();
            ViewCache = Result;
            BuildViewCache = null;
            CacheRebuildNeeded = false;
          } /*onPostExecute*/

      } /*ViewCacheBuilder*/

    protected void Init
      (
        android.content.Context Context
      )
      /* common code for all constructors */
      {
        this.Context = Context;
        ZoomFactor = 1.0f;
        ScrollX = 0.5f;
        ScrollY = 0.5f;
        setHorizontalFadingEdgeEnabled(true);
        setVerticalFadingEdgeEnabled(true);
      } /*Init*/

    public DrawView
      (
        android.content.Context Context
      )
      {
        super(Context);
        Init(Context);
      } /*DrawView*/

    public DrawView
      (
        android.content.Context Context,
        android.util.AttributeSet Attributes
      )
      {
        this(Context, Attributes, 0);
      } /*DrawView*/

    public DrawView
      (
        android.content.Context Context,
        android.util.AttributeSet Attributes,
        int DefaultStyle
      )
      {
        super(Context, Attributes, DefaultStyle);
        Init(Context);
      } /*DrawView*/

    protected static class SavedDrawViewState extends android.view.AbsSavedState
      {
        public static android.os.Parcelable.Creator<SavedDrawViewState> CREATOR =
            new android.os.Parcelable.Creator<SavedDrawViewState>()
              {
                public SavedDrawViewState createFromParcel
                  (
                    android.os.Parcel SavedState
                  )
                  {
                    System.err.println("DrawView.SavedDrawViewState.createFromParcel"); /* debug */
                    final android.view.AbsSavedState SuperState =<commit_msg>explanation of save/restore state mechanics<commit_after>package nz.gen.geek_central.view_cache_demo;
/*
    Demonstration of how to do smooth scrolling of a complex
    image by careful caching of bitmaps--actual view caching.

    Copyright 2011 by <NAME> <<EMAIL>>.

    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.
*/

import android.graphics.PointF;
import android.graphics.RectF;
import android.graphics.Bitmap;
import android.view.MotionEvent;

public class DrawView extends android.view.View
  {
    protected android.content.Context Context;
    protected float ZoomFactor;
    protected float ScrollX, ScrollY; /* [0.0 .. 1.0] */
    protected final float MaxZoomFactor = 32.0f;
    protected final float MinZoomFactor = 1.0f;

    protected Drawer DrawWhat;
    protected boolean UseCaching = true;

    protected static class ViewCacheBits
      {
        public final Bitmap Bits;
        public final RectF Bounds;

        public ViewCacheBits
          (
            Bitmap Bits,
            RectF Bounds
          )
          {
            this.Bits = Bits;
            this.Bounds = Bounds;
          } /*ViewCacheBits*/

      } /*ViewCacheBits*/

    protected PointF
        LastMouse1 = null,
        LastMouse2 = null;
    protected int
        Mouse1ID = -1,
        Mouse2ID = -1;
    protected final float MaxCacheFactor = 2.0f;
      /* how far to cache beyond visible bounds */
    protected ViewCacheBits ViewCache = null;
    protected ViewCacheBuilder BuildViewCache = null;
    protected boolean CacheRebuildNeeded = false;
    boolean MouseMoved = false;

    protected class ViewCacheBuilder extends android.os.AsyncTask<Void, Integer, ViewCacheBits>
      {
        protected RectF ScaledViewBounds, CacheBounds;

        protected void onPreExecute()
          {
            final ViewParms v = new ViewParms();
            ScaledViewBounds = new RectF(0, 0, v.ScaledViewWidth, v.ScaledViewHeight);
            final PointF ViewOffset = ScrollOffset(v);
            final PointF ViewCenter = new PointF
              (
                /*x =*/ v.ViewWidth / 2.0f - ViewOffset.x,
                /*y =*/ v.ViewHeight / 2.0f - ViewOffset.y
              );
            CacheBounds = new RectF
              (
                /*left =*/
                    Math.max
                      (
                        ViewCenter.x - v.ViewWidth * MaxCacheFactor / 2.0f,
                        ScaledViewBounds.left
                      ),
                /*top =*/
                    Math.max
                      (
                        ViewCenter.y - v.ViewHeight * MaxCacheFactor / 2.0f,
                        ScaledViewBounds.top
                      ),
                /*right =*/
                    Math.min
                      (
                        ViewCenter.x + v.ViewWidth * MaxCacheFactor / 2.0f,
                        ScaledViewBounds.right
                      ),
                /*bottom =*/
                    Math.min
                      (
                        ViewCenter.y + v.ViewHeight * MaxCacheFactor / 2.0f,
                        ScaledViewBounds.bottom
                      )
              );
            if (CacheBounds.isEmpty())
              {
              /* can seem to happen, e.g. on orientation change */
                cancel(true);
              } /*if*/
          } /*onPreExecute*/

        protected ViewCacheBits doInBackground
          (
            Void... Unused
          )
          {
            final Bitmap CacheBits =
                Bitmap.createBitmap
                  (
                    /*width =*/ (int)(CacheBounds.right - CacheBounds.left),
                    /*height =*/ (int)(CacheBounds.bottom - CacheBounds.top),
                    /*config =*/ Bitmap.Config.ARGB_8888
                  );
            final android.graphics.Canvas CacheDraw = new android.graphics.Canvas(CacheBits);
            final RectF DestRect = new RectF(ScaledViewBounds);
            DestRect.offset(- CacheBounds.left, - CacheBounds.top);
            DrawWhat.Draw(CacheDraw, DestRect); /* this is the time-consuming part */
            CacheBits.prepareToDraw();
            return
                new ViewCacheBits(CacheBits, CacheBounds);
          } /*doInBackground*/

        protected void onCancelled
          (
            ViewCacheBits Result
          )
          {
            Result.Bits.recycle();
          } /*onCancelled*/

        protected void onPostExecute
          (
            ViewCacheBits Result
          )
          {
            DisposeViewCache();
            ViewCache = Result;
            BuildViewCache = null;
            CacheRebuildNeeded = false;
          } /*onPostExecute*/

      } /*ViewCacheBuilder*/

    protected void Init
      (
        android.content.Context Context
      )
      /* common code for all constructors */
      {
        this.Context = Context;
        ZoomFactor = 1.0f;
        ScrollX = 0.5f;
        ScrollY = 0.5f;
        setHorizontalFadingEdgeEnabled(true);
        setVerticalFadingEdgeEnabled(true);
      } /*Init*/

    public DrawView
      (
        android.content.Context Context
      )
      {
        super(Context);
        Init(Context);
      } /*DrawView*/

    public DrawView
      (
        android.content.Context Context,
        android.util.AttributeSet Attributes
      )
      {
        this(Context, Attributes, 0);
      } /*DrawView*/

    public DrawView
      (
        android.content.Context Context,
        android.util.AttributeSet Attributes,
        int DefaultStyle
      )
      {
        super(Context, Attributes, DefaultStyle);
        Init(Context);
      } /*DrawView*/

  /*
    Implementation of saving/restoring instance state. Doing this
    allows me to transparently restore scroll/zoom state if system
    needs to kill me while I'm in the background.

    Notes: View.onSaveInstanceState returns AbsSavedState.EMPTY_STATE,
    and View.onRestoreInstanceState expects to be passed this. Also,
    both superclass methods MUST be called in my overrides (the docs
    don't make this clear).
  */

    protected static class SavedDrawViewState extends android.view.AbsSavedState
      {
        public static android.os.Parcelable.Creator<SavedDrawViewState> CREATOR =
            new android.os.Parcelable.Creator<SavedDrawViewState>()
              {
                public SavedDrawViewState createFromParcel
                  (
                    android.os.Parcel SavedState
                  )
                  {
                    System.err.println("DrawView.SavedDrawViewState.createFromParcel"); /* debug */
                    final android.view.AbsSavedState SuperState =
{'fraction_non_alphanumeric': 0.07779845405095906, 'fraction_numerical': 0.0071571714858288, 'mean_word_length': 1.5135815794207592, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '4892661', 'n_tokens_mistral': 3637, 'n_tokens_neox': 3429, 'n_words': 918}