Skip to content

Commit

Permalink
Show name in ShareActivity and some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nning committed May 28, 2017
1 parent 4cb9a1e commit 53f48a0
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 79 deletions.
23 changes: 16 additions & 7 deletions app/app.iml
Original file line number Diff line number Diff line change
Expand Up @@ -62,31 +62,40 @@
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/shaders" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/builds" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-runtime-classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-safeguard" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-verifier" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/instant-run-resources" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/instant-run-support" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/pre-dexed" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/reload-dex" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/restart-dex" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/shaders" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/split-apk" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/transforms" />
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
Expand Down
28 changes: 11 additions & 17 deletions app/src/main/java/net/orgizm/imgshr/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;

public class Connection
class Connection
{
private final String DEFAULT_API_URL = "https://imgshr.space/api";

Expand All @@ -44,27 +44,22 @@ public class Connection
private OutputStream out;
private NotificationManager nManager;
private NotificationCompat.Builder nBuilder;
private String slug;

private final String PARAM = "picture[image][]";
private final String BOUNDARY = "*****";
private final String CRLF = "\r\n";

private int nId = 0;

public Connection(Context context, String slug) throws Exception {
Connection(Context context, String slug) throws Exception {
this(context, slug, null, null, null, 0);
}

public Connection(Context context, String slug, NotificationManager nManager, NotificationCompat.Builder nBuilder) throws Exception {
this(context, slug, null, nManager, nBuilder, 0);
}

public Connection(Context context, String slug, NotificationManager nManager, NotificationCompat.Builder nBuilder, int nId) throws Exception {
Connection(Context context, String slug, NotificationManager nManager, NotificationCompat.Builder nBuilder, int nId) throws Exception {
this(context, slug, null, nManager, nBuilder, nId);
}

public Connection(Context context, String slug, String endpoint, NotificationManager nManager, NotificationCompat.Builder nBuilder, int nId) throws Exception {
Connection(Context context, String slug, String endpoint, NotificationManager nManager, NotificationCompat.Builder nBuilder, int nId) throws Exception {
URL url;

Boolean https = false;
Expand All @@ -83,7 +78,6 @@ public Connection(Context context, String slug, String endpoint, NotificationMan
this.nManager = nManager;
this.nBuilder = nBuilder;
this.nId = nId;
this.slug = slug;

url = new URL(endpoint + "/!" + slug);

Expand All @@ -95,7 +89,7 @@ public Connection(Context context, String slug, String endpoint, NotificationMan
}
}

public void disconnect() {
void disconnect() {
conn.disconnect();
}

Expand All @@ -113,10 +107,10 @@ private void uploadImage(Uri imageUri, int i, int n) throws IOException, Instant
out.write(CRLF.getBytes());

byte[] buffer = new byte[256];
int bytesRead = 0;
int bytesRead;
int size = file.available();
int written = 0;
int percent = 0;
int percent;
int lastPercent = 0;

String overall = "";
Expand All @@ -142,7 +136,7 @@ private void uploadImage(Uri imageUri, int i, int n) throws IOException, Instant
out.write(CRLF.getBytes());
}

public String uploadImages(ArrayList<Uri> imageUris) throws IOException, InstantiationException, IllegalAccessException {
String uploadImages(ArrayList<Uri> imageUris) throws IOException, InstantiationException, IllegalAccessException {
setConnectionPropertiesForUpload();
out = new BufferedOutputStream(conn.getOutputStream());

Expand Down Expand Up @@ -237,7 +231,7 @@ public boolean verify(String hostname, SSLSession session) {
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
}

public void setConnectionPropertiesForUpload() throws ProtocolException {
private void setConnectionPropertiesForUpload() throws ProtocolException {
conn.setDoOutput(true);
conn.setChunkedStreamingMode(0);

Expand All @@ -246,11 +240,11 @@ public void setConnectionPropertiesForUpload() throws ProtocolException {
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + BOUNDARY);
}

public void setConnectionPropertiesForDiscover() throws ProtocolException {
private void setConnectionPropertiesForDiscover() throws ProtocolException {
conn.setRequestMethod("GET");
}

public String discoverGallery() throws IOException {
String discoverGallery() throws IOException {
setConnectionPropertiesForDiscover();

int code = conn.getResponseCode();
Expand Down
17 changes: 8 additions & 9 deletions app/src/main/java/net/orgizm/imgshr/Gallery.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package net.orgizm.imgshr;

import android.support.annotation.NonNull;

import com.google.gson.Gson;

public class Gallery implements Comparable<Gallery> {
class Gallery implements Comparable<Gallery> {
private String name;
private String slug;

public Gallery() {
}

public Gallery(String slug) {
Gallery(String slug) {
this.slug = slug;
}

Expand Down Expand Up @@ -37,17 +36,17 @@ public String toString() {
}
}

public void updateDetails(Gallery gallery) {
void updateDetails(Gallery gallery) {
this.name = gallery.getName();
}

public void updateDetails(String json) {
void updateDetails(String json) {
final Gallery gallery = new Gson().fromJson(json, Gallery.class);
updateDetails(gallery);
}

public int compareTo(Gallery other) {
if (this.getName() == other.getName()) {
public int compareTo(@NonNull Gallery other) {
if (this.getName().equals(other.getName())) {
return 0;
} else {
return -1;
Expand Down
16 changes: 1 addition & 15 deletions app/src/main/java/net/orgizm/imgshr/GalleryAddActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class GalleryAddActivity extends Activity
{
final String LOG_TARGET = "net.orgizm.imgshr";

private Context context;

private TextView slug;
Expand All @@ -32,25 +29,14 @@ public void addGalleryCallback(View view) {
new Thread(new Runnable() {
public void run() {
final String s = slug.getText().toString();
final Gallery gallery = new Gallery(s);

try {
final Connection conn = new Connection(context, s);
final String json = conn.discoverGallery();

gallery.updateDetails(json);
}
catch(Exception e) {
Log.d(LOG_TARGET, Log.getStackTraceString(e));
}
preferences.addGallery(context, s);

runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(context, R.string.gallery_saved, Toast.LENGTH_SHORT).show();
}
});

preferences.addGallery(gallery);
finish();
}
}).start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class GalleryListActivity extends Activity {
final String LOG_TARGET = "net.orgizm.imgshr";
Expand Down
13 changes: 6 additions & 7 deletions app/src/main/java/net/orgizm/imgshr/GalleryListAdapter.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.orgizm.imgshr;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
Expand All @@ -11,14 +10,14 @@

import java.util.List;

public class GalleryListAdapter extends RecyclerView.Adapter<GalleryListAdapter.ViewHolder> {
class GalleryListAdapter extends RecyclerView.Adapter<GalleryListAdapter.ViewHolder> {
private List<Gallery> galleriesList;
private int position;

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener, View.OnClickListener {
class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener, View.OnClickListener {
public TextView slug;

public ViewHolder(View view) {
ViewHolder(View view) {
super(view);

slug = (TextView) view.findViewById(R.id.slug);
Expand All @@ -43,7 +42,7 @@ public void onClick(View view) {
}
}

public GalleryListAdapter(List<Gallery> galleriesList) {
GalleryListAdapter(List<Gallery> galleriesList) {
this.galleriesList = galleriesList;
}

Expand All @@ -55,11 +54,11 @@ public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(itemView);
}

public int getPosition() {
int getPosition() {
return position;
}

public void setPosition(int position) {
private void setPosition(int position) {
this.position = position;
}

Expand Down
46 changes: 26 additions & 20 deletions app/src/main/java/net/orgizm/imgshr/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@
import java.util.Set;

public class Preferences {
final String PREFERENCES_NAME = "imgshr";
final String GALLERIES_KEY = "galleries";
final private String PREFERENCES_NAME = "imgshr";
final private String GALLERIES_KEY = "galleries";
final private String LOG_TARGET = "net.orgizm.imgshr";

private SharedPreferences preferences;
private Gson gson = new Gson();

public Preferences(Context context) {
Preferences(Context context) {
preferences = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
}

public Set<Gallery> getGalleries() {
Set<Gallery> getGalleries() {
Set<String> serializedGalleries = preferences.getStringSet(GALLERIES_KEY, null);
Set<Gallery> galleries = new HashSet<>();

Expand All @@ -36,7 +37,7 @@ public Set<Gallery> getGalleries() {
return galleries;
}

public Gallery[] getGalleriesAsArray() {
Gallery[] getGalleriesAsArray() {
Set<Gallery> galleries = getGalleries();

if (galleries == null) {
Expand All @@ -46,18 +47,7 @@ public Gallery[] getGalleriesAsArray() {
}
}

public Set<String> getLastSlugs() {
Set<Gallery> galleries = getGalleries();
Set<String> slugs = new HashSet<>();

for (Gallery gallery : galleries) {
slugs.add(gallery.getSlug());
}

return slugs;
}

public void addGallery(Gallery gallery) {
void addGallery(Gallery gallery) {
if (gallery == null) return;

List<Gallery> newGalleries = new ArrayList<>(getGalleries());
Expand All @@ -81,17 +71,33 @@ public void addGallery(Gallery gallery) {
setGalleries(newGalleries);
}

public void setGalleries(List<Gallery> galleries) {
void addGallery(Context context, String slug) {
final Gallery gallery = new Gallery(slug);

try {
final Connection conn = new Connection(context, slug);
final String json = conn.discoverGallery();

gallery.updateDetails(json);
}
catch(Exception e) {
Log.d(LOG_TARGET, Log.getStackTraceString(e));
}

addGallery(gallery);
}

void setGalleries(List<Gallery> galleries) {
setGalleries(new HashSet<>(galleries));
}

public void setGalleries(Set<Gallery> galleries) {
void setGalleries(Set<Gallery> galleries) {
SharedPreferences.Editor editor = preferences.edit();
editor.putStringSet(GALLERIES_KEY, serializeGalleries(galleries));
editor.apply();
}

public Set<String> serializeGalleries(Set<Gallery> galleries) {
Set<String> serializeGalleries(Set<Gallery> galleries) {
Set<String> serializedGalleries = new HashSet<>();

for (Gallery gallery : galleries) {
Expand Down
Loading

0 comments on commit 53f48a0

Please sign in to comment.