Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Target API 25, fix issue #46, use OkHttp for all http calls #73

Merged
merged 2 commits into from
Dec 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ gen
/*.eml
/*.ipr
/*.iws

local.properties
55 changes: 35 additions & 20 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,69 @@ apply plugin: 'com.android.application'

android {
Properties props = new Properties()
props.load(new FileInputStream(System.getProperty("user.home")+"/androidkeyinfo.properties"))
compileSdkVersion 22
buildToolsVersion "22.0.1"

def propsPath = System.getProperty("user.home") + "/androidkeyinfo.properties"

if (new File(propsPath).exists())
props.load(new FileInputStream(propsPath))

compileSdkVersion 25
buildToolsVersion "25.0.1"

defaultConfig {
minSdkVersion 9
targetSdkVersion 22
targetSdkVersion 25
testInstrumentationRunner "android.test.InstrumentationTestRunner"
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}

signingConfigs {
debug {
storeFile file(System.getProperty("user.home")+"/androidkeystore")
debugSigned {
storeFile file(System.getProperty("user.home") + "/androidkeystore")
storePassword props.getProperty("storePassword")
keyAlias "robert \"kebernet\" cooper"
keyPassword props.getProperty("keyPassword")
}
release {
storeFile file(System.getProperty("user.home")+"/androidkeystore")
storeFile file(System.getProperty("user.home") + "/androidkeystore")
storePassword props.getProperty("storePassword")
keyAlias "robert \"kebernet\" cooper"
keyPassword props.getProperty("keyPassword")
}
}

buildTypes {
debug {
}
debugSigned {
signingConfig signingConfigs.debugSigned
}
release {
signingConfig signingConfigs.release
minifyEnabled true
proguardFile file('src/main/assemble/proguard-android.txt')
signingConfig signingConfigs.release
minifyEnabled true
proguardFile file('src/main/assemble/proguard-android.txt')
}
}
}

dependencies {
compile 'com.android.support:appcompat-v7:22.1.1'
compile 'com.android.support:recyclerview-v7:22.2.0'
compile 'com.android.support:appcompat-v7:25.0.1'
compile 'com.android.support:recyclerview-v7:25.0.1'
compile 'com.google.android.gms:play-services:4.0.30'
compile project(":puzlib")
compile 'com.nineoldandroids:library:2.4.0'
compile 'com.jenzz:materialpreference:1.3'
compile 'com.android.support:design:22.2.0'
compile 'com.android.support:design:25.0.1'
compile 'com.google.api-client:google-api-client:1.20.0'
compile 'com.google.api-client:google-api-client-android:1.20.0'
compile 'com.google.api-client:google-api-client-gson:1.20.0'
compile 'com.google.apis:google-api-services-gmail:v1-rev29-1.20.0'
androidTestCompile 'com.jayway.android.robotium:robotium-solo:4.3.1'
compile 'com.squareup.okhttp3:okhttp:3.2.0'

}

task prepareChrome << {
Expand All @@ -60,7 +77,7 @@ task prepareChrome << {
include "**/*.html"
filter {
it.replaceAll('@@versionCode@@', versionCode)
.replaceAll('@@versionName@@', versionName)
.replaceAll('@@versionName@@', versionName)
}
}
into "build/chrome"
Expand All @@ -80,15 +97,13 @@ task assembleChrome << {
into "build/chrome/vendor/chromium/crx"
}
}
tasks.assembleChrome.dependsOn prepareChrome
tasks.assembleChrome.dependsOn assembleRelease
tasks.assembleChrome.dependsOn "prepareChrome"
tasks.assembleChrome.dependsOn "assembleRelease"

task packageChrome(type:Zip){
task packageChrome(type: Zip) {
from "build/chrome"
archiveName "shortyz-chrome.zip"
}
tasks.packageChrome.dependsOn assembleChrome

tasks.build.dependsOn packageChrome


53 changes: 51 additions & 2 deletions app/src/main/java/com/totsp/crossword/BrowseActivity.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package com.totsp.crossword;

import android.Manifest;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.NotificationManager;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.view.ActionMode;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
Expand Down Expand Up @@ -60,6 +67,7 @@
public class BrowseActivity extends ShortyzActivity implements RecyclerItemClickListener.OnItemClickListener{
private static final String MENU_ARCHIVES = "Archives";
private static final int DOWNLOAD_DIALOG_ID = 0;
private static final int REQUEST_WRITE_STORAGE = 1002;
private static final long DAY = 24L * 60L * 60L * 1000L;
private static final Logger LOGGER = Logger.getLogger(BrowseActivity.class.getCanonicalName());
private Accessor accessor = Accessor.DATE_DESC;
Expand All @@ -78,6 +86,7 @@ public class BrowseActivity extends ShortyzActivity implements RecyclerItemClick
private boolean viewArchive;
private MenuItem gamesItem;
private boolean signedIn;
private boolean hasWritePermissions;
private int playIcon = R.drawable.ic_play_games_badge_green;
private FloatingActionButton download;
private int highlightColor;
Expand Down Expand Up @@ -402,15 +411,47 @@ public void onClick(View v) {

}



highlightColor = getResources().getColor(R.color.accent);
normalColor = getResources().getColor(R.color.background_material_light);
primaryTextColor = getResources().getColor(R.color.textColorPrimary);

if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
new AlertDialog.Builder(this)
.setTitle("Allow Permissions")
.setMessage("Please allow writing to storage when prompted. Shortyz needs this permission to store downloaded crossword files and cannot work without it.")
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ActivityCompat.requestPermissions(BrowseActivity.this, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, REQUEST_WRITE_STORAGE);
}
})
.create()
.show();
} else {
ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, REQUEST_WRITE_STORAGE);
}

return;
} else {
hasWritePermissions = true;
}

startInitialActivityOrFinishLoading();
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_WRITE_STORAGE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
hasWritePermissions = true;
startInitialActivityOrFinishLoading();
}
}
}

private void startInitialActivityOrFinishLoading() {
if (!crosswordsFolder.exists()) {
this.downloadTen();

Expand Down Expand Up @@ -612,6 +653,8 @@ public void run(){
}

private void checkDownload() {
if (!hasWritePermissions) return;

long lastDL = prefs.getLong("dlLast", 0);

if (prefs.getBoolean("dlOnStartup", true) &&
Expand Down Expand Up @@ -708,6 +751,8 @@ private void moveTo(File puzFile, File directory){
}

private void download(final Date d, final List<Downloader> downloaders, final boolean scrape) {
if (!hasWritePermissions) return;

final Downloaders dls = new Downloaders(prefs, nm, this);
LOGGER.info("Downloading from "+downloaders);
new Thread(new Runnable() {
Expand All @@ -729,6 +774,8 @@ public void run() {
}

private void downloadTen() {
if (!hasWritePermissions) return;

new Thread(new Runnable() {
public void run() {
Downloaders dls = new Downloaders(prefs, nm, BrowseActivity.this);
Expand All @@ -754,6 +801,8 @@ public void run() {
}

private void render() {
if (!hasWritePermissions) return;

if ((this.sources != null) && (this.sources.getAdapter() == null)) {
final SourceListAdapter adapter = new SourceListAdapter(this, this.sourceList);
this.sources.setAdapter(adapter);
Expand Down
70 changes: 55 additions & 15 deletions app/src/main/java/com/totsp/crossword/HttpDownloadActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,30 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.widget.Toast;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class HttpDownloadActivity extends Activity {

private static final int REQUEST_EXTERNAL_STORAGE = 1001;

private File crosswordsFolder = new File(Environment.getExternalStorageDirectory(), "crosswords");

/**
Expand Down Expand Up @@ -61,6 +68,40 @@ public static int copyStream(InputStream sourceStream, OutputStream destinationS
public void onCreate(Bundle icicle) {
super.onCreate(icicle);

if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
new AlertDialog.Builder(this)
.setTitle("Allow Permissions")
.setMessage("Please allow writing to storage when prompted. Shortyz needs this permission to store downloaded crossword files and cannot work without it.")
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ActivityCompat.requestPermissions(HttpDownloadActivity.this, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, REQUEST_EXTERNAL_STORAGE);
}
})
.create()
.show();
} else {
ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, REQUEST_EXTERNAL_STORAGE);
}

return;
}

initializeDownload();
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_EXTERNAL_STORAGE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
initializeDownload();
}
}
}

private void initializeDownload() {
if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
showSDCardHelp();
finish();
Expand All @@ -77,21 +118,20 @@ public void onCreate(Bundle icicle) {
dialog.setMessage("Downloading...\n" + filename);
dialog.setCancelable(false);

HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
OkHttpClient client = new OkHttpClient();

try {
request.setURI(new URI(u.toString()));
Request request = new Request.Builder()
.url(u.toString())
.build();

HttpResponse response = client.execute(request);
Response response = client.newCall(request).execute();

if (response.getStatusLine()
.getStatusCode() != 200) {
if (response.code() != 200) {
throw new IOException("Non 200 downloading...");
}

InputStream is = response.getEntity()
.getContent();
InputStream is = response.body().byteStream();
File puzFile = new File(crosswordsFolder, filename);
FileOutputStream fos = new FileOutputStream(puzFile);
copyStream(is, fos);
Expand All @@ -102,7 +142,7 @@ public void onCreate(Bundle icicle) {
} catch (Exception e) {
e.printStackTrace();

Toast t = Toast.makeText(this, "Unabled to download from\n" + u.toString(), Toast.LENGTH_LONG);
Toast t = Toast.makeText(this, "Unable to download from\n" + u.toString(), Toast.LENGTH_LONG);
t.show();
}

Expand Down
6 changes: 4 additions & 2 deletions app/src/main/java/com/totsp/crossword/PlayActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,10 @@ public void swipeUp() {
this.clue = (TextView) this.findViewById(R.id.clueLine);
if (clue != null && clue.getVisibility() != View.GONE) {
clue.setVisibility(View.GONE);
clue = (TextView) utils.onActionBarCustom(this,
R.layout.clue_line_only).findViewById(R.id.clueLine);
View custom = utils.onActionBarCustom(this, R.layout.clue_line_only);
if (custom != null) {
clue = (TextView) custom.findViewById(R.id.clueLine);
}
}
if(this.clue != null) {
this.clue.setClickable(true);
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/java/com/totsp/crossword/firstrun/NYTimes.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import com.totsp.crossword.shortyz.R;
import com.totsp.crossword.shortyz.ShortyzApplication;

import org.apache.http.client.HttpClient;

import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.regex.Pattern;
Expand Down
Loading