Skip to content

Commit

Permalink
Hack screenshots for Android >= 5.0 Lollipop
Browse files Browse the repository at this point in the history
  • Loading branch information
modos189 committed Apr 21, 2019
1 parent 30b0ff4 commit 6677855
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 24 deletions.
69 changes: 45 additions & 24 deletions mobile/app/src/main/java/org/exarhteam/iitc_mobile/IITC_Mobile.java
Expand Up @@ -38,6 +38,7 @@
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.TextView;
Expand All @@ -49,6 +50,7 @@
import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.MenuItemCompat;
import androidx.drawerlayout.widget.DrawerLayout;

import org.exarhteam.iitc_mobile.IITC_NavigationHelper.Pane;
import org.exarhteam.iitc_mobile.prefs.PreferenceActivity;
Expand Down Expand Up @@ -680,7 +682,7 @@ public boolean onOptionsItemSelected(final MenuItem item) {
cm.removeAllCookie();
return true;
case R.id.menu_send_screenshot:
sendScreenshot();
sendScreenshot(this);
return true;
case R.id.menu_debug:
mDebugging = !mDebugging;
Expand Down Expand Up @@ -962,32 +964,51 @@ public void setPermalink(final String href) {
mPermalink = href;
}

private void sendScreenshot() {
Bitmap bitmap = Bitmap.createBitmap(mIitcWebView.getMeasuredWidth(),
mIitcWebView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
private void sendScreenshot(final IITC_Mobile iitc) {
Toast.makeText(this, "3…", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(() -> Toast.makeText(iitc, "2…", Toast.LENGTH_SHORT).show(), 1000);
new Handler().postDelayed(() -> Toast.makeText(iitc, "1…", Toast.LENGTH_SHORT).show(), 2000);
new Handler().postDelayed(() -> Toast.makeText(iitc, R.string.msg_take_screenshot, Toast.LENGTH_SHORT).show(), 2900);

Canvas bigcanvas = new Canvas(bitmap);
Paint paint = new Paint();
int iHeight = bitmap.getHeight();
bigcanvas.drawBitmap(bitmap, 0, iHeight, paint);
mIitcWebView.draw(bigcanvas);
// Hack for Android >= 5.0 Lollipop
// When hardware acceleration is enabled, it is not possible to create a screenshot.
// After switch to software render, we need to redraw the webview, but of all the ways I have worked only resizing.
// This takes some time, so a timer is set.
// After the screenshot is taken, the webview size and render type are returned to their original state.
mIitcWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
mIitcWebView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, mIitcWebView.getMeasuredHeight()+50));

try {
final File cache = getExternalCacheDir();
final File file = File.createTempFile("IITC screenshot", ".png", cache);
if (!bitmap.compress(CompressFormat.PNG, 100, new FileOutputStream(file))) {
// quality is ignored by PNG
throw new IOException("Could not compress bitmap!");
}
startActivityForResult(ShareActivity.forFile(this, file, "image/png"), new ResponseHandler() {
@Override
public void onActivityResult(final int resultCode, final Intent data) {
file.delete();
Handler handler = new Handler();
handler.postDelayed(() -> {
Bitmap bitmap = Bitmap.createBitmap(mIitcWebView.getMeasuredWidth(),
mIitcWebView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

Canvas bigcanvas = new Canvas(bitmap);
Paint paint = new Paint();
int iHeight = bitmap.getHeight();
bigcanvas.drawBitmap(bitmap, 0, iHeight, paint);

mIitcWebView.draw(bigcanvas);

try {
final File cache = getExternalCacheDir();
final File file = File.createTempFile("IITC screenshot", ".png", cache);
if (!bitmap.compress(CompressFormat.PNG, 100, new FileOutputStream(file))) {
// quality is ignored by PNG
throw new IOException("Could not compress bitmap!");
}
});
} catch (final IOException e) {
Log.e("Could not generate screenshot", e);
}
startActivityForResult(ShareActivity.forFile(iitc, file, "image/png"), (resultCode, data) -> {
mIitcWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
mIitcWebView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.FILL_PARENT));

file.delete();
});
} catch (final IOException e) {
Log.e("Could not generate screenshot", e);
}

}, 3000);

}

@Override
Expand Down
1 change: 1 addition & 0 deletions mobile/app/src/main/res/values/strings.xml
Expand Up @@ -153,6 +153,7 @@
<string name="search_hint">Search locations</string>
<string name="intent_error">Address could not be opened</string>
<string name="msg_copied">Copied to clipboard…</string>
<string name="msg_take_screenshot">Takes a screenshot…</string>
<string name="notice_do_not_show_again">Do not show again</string>

<string name="sign_in_to">Sign in to %1$s %2$s"</string>
Expand Down

0 comments on commit 6677855

Please sign in to comment.