Skip to content

Commit

Permalink
Get gallery name when adding via GalleryListActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
nning committed May 21, 2017
1 parent f725ff4 commit 6be9eed
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 24 deletions.
1 change: 0 additions & 1 deletion app/app.iml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@
<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" />
Expand Down
68 changes: 56 additions & 12 deletions app/src/main/java/net/orgizm/imgshr/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
Expand All @@ -37,18 +38,25 @@

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

private Context context;
private HttpURLConnection conn;
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 {
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);
}
Expand All @@ -64,7 +72,7 @@ public Connection(Context context, String slug, String endpoint, NotificationMan
Boolean pinning = false;

if (endpoint == null) {
endpoint = "https://imgshr.space/api";
endpoint = DEFAULT_API_URL;
pinning = true;
}

Expand All @@ -76,6 +84,7 @@ 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 @@ -85,17 +94,13 @@ public Connection(Context context, String slug, String endpoint, NotificationMan
} else {
conn = (HttpURLConnection) url.openConnection();
}

setConnectionProperties();

out = new BufferedOutputStream(conn.getOutputStream());
}
}

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

private void uploadImage(Uri imageUri, int i, int n) throws FileNotFoundException, IOException, InstantiationException, IllegalAccessException {
private void uploadImage(Uri imageUri, int i, int n) throws IOException, InstantiationException, IllegalAccessException {
ContentResolver cr = context.getContentResolver();
InputStream file = cr.openInputStream(imageUri);

Expand Down Expand Up @@ -138,8 +143,11 @@ private void uploadImage(Uri imageUri, int i, int n) throws FileNotFoundExceptio
out.write(CRLF.getBytes());
}

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

int n = imageUris.size();
for(int i = 0; i < n; i++) {
uploadImage(imageUris.get(i), i, n);
}
Expand All @@ -150,7 +158,7 @@ public String uploadImages(ArrayList<Uri> imageUris) throws FileNotFoundExceptio
int code = conn.getResponseCode();
String message = conn.getResponseMessage();

Log.i("net.orgizm.imgshr", "HTTP Response: " + code + " " + message);
Log.i("net.orgizm.imgshr", "HTTP upload Response: " + code + " " + message);

return code + " " + message;
}
Expand All @@ -176,7 +184,7 @@ private String getFileName(ContentResolver cr, Uri uri) {

private void initializeEncryption(Boolean pinning) throws Exception {
if (pinning) {
initializePinning();
initializePinning();
} else {
initializeTrustAllCerts();
}
Expand Down Expand Up @@ -230,12 +238,48 @@ public boolean verify(String hostname, SSLSession session) {
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
}

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

conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + BOUNDARY);
}

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

public String discoverGallery() throws IOException {
final Gallery gallery = new Gallery(slug);

setConnectionPropertiesForDiscover();

int code = conn.getResponseCode();
String message = conn.getResponseMessage();

Log.i("net.orgizm.imgshr", "HTTP discover Response: " + code + " " + message);

String json = "";

try {
InputStream in = conn.getInputStream();
InputStreamReader reader = new InputStreamReader(in);

int data = reader.read();
while (data != -1) {
json += (char) data;
data = reader.read();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();
}
}

return json;
}
}
19 changes: 19 additions & 0 deletions app/src/main/java/net/orgizm/imgshr/Gallery.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package net.orgizm.imgshr;

import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;

public class Gallery {
private String name;
private String slug;

public Gallery() {
Expand All @@ -10,11 +16,24 @@ public Gallery(String slug) {
this.slug = slug;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSlug() {
return slug;
}

public void setSlug(String slug) {
this.slug = slug;
}

public void updateDetailsFromJson(String json) {
final Gallery gallery = new Gson().fromJson(json, Gallery.class);
this.name = gallery.getName();
}
}
41 changes: 36 additions & 5 deletions app/src/main/java/net/orgizm/imgshr/GalleryAddActivity.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package net.orgizm.imgshr;

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
{
private TextView slug;
final String LOG_TARGET = "net.orgizm.imgshr";

private Context context;

private TextView slug;
private Preferences preferences;

@Override
Expand All @@ -17,13 +23,38 @@ public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery_add_activity);

context = getApplicationContext();
slug = (TextView) findViewById(R.id.slug);
preferences = new Preferences(getApplicationContext());
preferences = new Preferences(context);
}

public void addGalleryCallback(View view) {
preferences.setLastSlugs(new Gallery(slug.getText().toString()));
Toast.makeText(getApplicationContext(), R.string.gallery_saved, Toast.LENGTH_SHORT).show();
finish();
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.updateDetailsFromJson(json);

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

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

preferences.setLastSlugs(gallery);
finish();
}
}).start();
}
}
8 changes: 3 additions & 5 deletions app/src/main/java/net/orgizm/imgshr/GalleryListActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,12 @@ protected void populateListView() {
list.setItemAnimator(new DefaultItemAnimator());
list.setAdapter(adapter);

Set<String> lastSlugs = preferences.getLastSlugs();
Set<Gallery> galleries = preferences.getGalleries();

galleriesList.clear();

if (lastSlugs != null) {
for (String slug : lastSlugs) {
galleriesList.add(new Gallery(slug));
}
for (Gallery gallery : galleries) {
galleriesList.add(gallery);
}

adapter.notifyDataSetChanged();
Expand Down
10 changes: 9 additions & 1 deletion app/src/main/java/net/orgizm/imgshr/GalleryListAdapter.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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 Down Expand Up @@ -63,7 +64,14 @@ public void setPosition(int position) {
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
Gallery gallery = galleriesList.get(position);
holder.slug.setText(gallery.getSlug());

String slug = gallery.getSlug();
String name = gallery.getName();

Log.d("net.orgizm.imgshr", "------------------> " + name);
Log.d("net.orgizm.imgshr", "------------------> " + (name == null ? slug : name));

holder.slug.setText(name == null ? slug : name);

holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/net/orgizm/imgshr/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.google.gson.Gson;

Expand Down

0 comments on commit 6be9eed

Please sign in to comment.