-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Targeting API 28. Added workaround for pruned canvas save(int) method…
- Loading branch information
Showing
6 changed files
with
133 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...ary/src/main/java/com/sothree/slidinguppanel/canvassaveproxy/AndroidPCanvasSaveProxy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.sothree.slidinguppanel.canvassaveproxy; | ||
|
||
import android.graphics.Canvas; | ||
import android.os.Build; | ||
import android.support.annotation.RequiresApi; | ||
import android.util.Log; | ||
|
||
@RequiresApi(api = Build.VERSION_CODES.P) | ||
class AndroidPCanvasSaveProxy implements CanvasSaveProxy { | ||
private static final String TAG = CanvasSaveProxy.class.getSimpleName(); | ||
private final Canvas mCanvas; | ||
|
||
AndroidPCanvasSaveProxy(final Canvas canvas) { | ||
Log.d(TAG, "New AndroidPCanvasSaveProxy"); | ||
|
||
mCanvas = canvas; | ||
} | ||
|
||
@Override | ||
public int save() { | ||
return mCanvas.save(); | ||
} | ||
|
||
@Override | ||
public boolean isFor(final Canvas canvas) { | ||
return canvas == mCanvas; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
library/src/main/java/com/sothree/slidinguppanel/canvassaveproxy/CanvasSaveProxy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.sothree.slidinguppanel.canvassaveproxy; | ||
|
||
import android.graphics.Canvas; | ||
|
||
public interface CanvasSaveProxy { | ||
int save(); | ||
|
||
boolean isFor(final Canvas canvas); | ||
} |
16 changes: 16 additions & 0 deletions
16
library/src/main/java/com/sothree/slidinguppanel/canvassaveproxy/CanvasSaveProxyFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.sothree.slidinguppanel.canvassaveproxy; | ||
|
||
import android.graphics.Canvas; | ||
import android.os.Build; | ||
|
||
public class CanvasSaveProxyFactory { | ||
|
||
public CanvasSaveProxy create(final Canvas canvas) { | ||
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { | ||
return new AndroidPCanvasSaveProxy(canvas); | ||
} else { | ||
return new LegacyCanvasSaveProxy(canvas); | ||
} | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
library/src/main/java/com/sothree/slidinguppanel/canvassaveproxy/LegacyCanvasSaveProxy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.sothree.slidinguppanel.canvassaveproxy; | ||
|
||
import android.graphics.Canvas; | ||
import android.util.Log; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
|
||
@Deprecated | ||
class LegacyCanvasSaveProxy implements CanvasSaveProxy { | ||
private static final String TAG = CanvasSaveProxy.class.getSimpleName(); | ||
private static final String METHOD_NAME = "save"; | ||
private static final String FIELD_NAME = "CLIP_SAVE_FLAG"; | ||
|
||
private final Canvas mCanvas; | ||
private final Method mSaveMethod; | ||
private final int mClipSaveFlag; | ||
|
||
LegacyCanvasSaveProxy(final Canvas canvas) { | ||
Log.d(TAG, "New LegacyCanvasSaveProxy"); | ||
|
||
mCanvas = canvas; | ||
mSaveMethod = findSaveMethod(); | ||
mClipSaveFlag = getClipSaveFlagValue(); | ||
} | ||
|
||
@Override | ||
public int save() { | ||
return invokeSave(); | ||
} | ||
|
||
@Override | ||
public boolean isFor(final Canvas canvas) { | ||
return canvas == mCanvas; | ||
} | ||
|
||
private int getClipSaveFlagValue() { | ||
final Field constantField; | ||
try { | ||
constantField = Canvas.class.getDeclaredField(FIELD_NAME); | ||
return (int) constantField.get(null); | ||
} catch (NoSuchFieldException e) { | ||
throw new IllegalStateException("Failed to get value of " + FIELD_NAME + " - NoSuchFieldException", e); | ||
} catch (IllegalAccessException e) { | ||
throw new IllegalStateException("Failed to get value of " + FIELD_NAME + " - IllegalAccessException", e); | ||
} | ||
} | ||
|
||
private Method findSaveMethod() { | ||
try { | ||
return Canvas.class.getMethod(METHOD_NAME, int.class); | ||
} catch (NoSuchMethodException e) { | ||
throw new IllegalStateException("Canvas does not contain a method with signature save(int)"); | ||
} | ||
} | ||
|
||
private int invokeSave() { | ||
try { | ||
return (int) mSaveMethod.invoke(mCanvas, mClipSaveFlag); | ||
} catch (IllegalAccessException e) { | ||
throw new IllegalStateException("Failed to execute save(int) - IllegalAccessException", e); | ||
} catch (InvocationTargetException e) { | ||
throw new IllegalStateException("Failed to execute save(int) - InvocationTargetException", e); | ||
} | ||
} | ||
} |