Created
January 31, 2015 15:48
-
-
Save lmccart/3cb3b68e92bfcf54fedc to your computer and use it in GitHub Desktop.
glass stash
This file contains hidden or 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
package com.example.lmccart.glassolalia; | |
import com.google.android.glass.media.Sounds; | |
import com.google.android.glass.widget.CardBuilder; | |
import com.google.android.glass.widget.CardScrollAdapter; | |
import com.google.android.glass.widget.CardScrollView; | |
import com.google.android.glass.content.Intents; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.media.AudioManager; | |
import android.os.Bundle; | |
import android.os.StrictMode; | |
import android.util.Base64; | |
import android.util.Log; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.AdapterView; | |
import android.content.Intent; | |
import android.provider.MediaStore; | |
import android.os.FileObserver; | |
import java.io.ByteArrayOutputStream; | |
import java.io.File; | |
import java.io.IOException; | |
import java.security.Timestamp; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.apache.http.HttpEntity; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.client.ClientProtocolException; | |
import org.apache.http.client.HttpClient; | |
import org.apache.http.client.entity.UrlEncodedFormEntity; | |
import org.apache.http.client.methods.HttpPost; | |
import org.apache.http.entity.ContentType; | |
import org.apache.http.entity.mime.HttpMultipartMode; | |
import org.apache.http.entity.mime.MultipartEntity; | |
import org.apache.http.entity.mime.MultipartEntityBuilder; | |
import org.apache.http.entity.mime.content.ByteArrayBody; | |
import org.apache.http.entity.mime.content.StringBody; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.NameValuePair; | |
import org.apache.http.message.BasicNameValuePair; | |
import org.apache.http.entity.mime.content.FileBody; | |
import org.apache.http.entity.FileEntity; | |
import org.apache.http.util.EntityUtils; | |
/** | |
* An {@link Activity} showing a tuggable "Hello World!" card. | |
* <p/> | |
* The main content view is composed of a one-card {@link CardScrollView} that provides tugging | |
* feedback to the user when swipe gestures are detected. | |
* If your Glassware intends to intercept swipe gestures, you should set the content view directly | |
* and use a {@link com.google.android.glass.touchpad.GestureDetector}. | |
* | |
* @see <a href="https://developers.google.com/glass/develop/gdk/touch">GDK Developer Guide</a> | |
*/ | |
public class Glassolalia extends Activity { | |
/** | |
* {@link CardScrollView} to use as the main content view. | |
*/ | |
private CardScrollView mCardScroller; | |
/** | |
* "Hello World!" {@link View} generated by {@link #buildView()}. | |
*/ | |
private View mView; | |
private static final int TAKE_PICTURE_REQUEST = 1; | |
@Override | |
protected void onCreate(Bundle bundle) { | |
super.onCreate(bundle); | |
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); | |
StrictMode.setThreadPolicy(policy); | |
mView = buildView(); | |
mCardScroller = new CardScrollView(this); | |
mCardScroller.setAdapter(new CardScrollAdapter() { | |
@Override | |
public int getCount() { | |
return 1; | |
} | |
@Override | |
public Object getItem(int position) { | |
return mView; | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
return mView; | |
} | |
@Override | |
public int getPosition(Object item) { | |
if (mView.equals(item)) { | |
return 0; | |
} | |
return AdapterView.INVALID_POSITION; | |
} | |
}); | |
// Handle the TAP event. | |
mCardScroller.setOnItemClickListener(new AdapterView.OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | |
// Plays disallowed sound to indicate that TAP actions are not supported. | |
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE); | |
am.playSoundEffect(Sounds.DISALLOWED); | |
takePicture(); | |
} | |
}); | |
setContentView(mCardScroller); | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
mCardScroller.activate(); | |
} | |
@Override | |
protected void onPause() { | |
mCardScroller.deactivate(); | |
super.onPause(); | |
} | |
/** | |
* Builds a Glass styled "Hello World!" view using the {@link CardBuilder} class. | |
*/ | |
private View buildView() { | |
CardBuilder card = new CardBuilder(this, CardBuilder.Layout.TEXT); | |
card.setText("hihihi"); | |
return card.getView(); | |
} | |
/** | |
* Make post. | |
*/ | |
private void makePost(File file) { | |
try { | |
HttpClient httpclient = new DefaultHttpClient(); | |
HttpPost httpPost = new HttpPost("http://lauren-mccarthy.com/test/uploader.php"); | |
String boundary = "-------------" + System.currentTimeMillis(); | |
httpPost.setHeader("Content-type", "multipart/form-data; boundary="+boundary); | |
byte[] bytes = org.apache.commons.io.FileUtils.readFileToByteArray(file); | |
// | |
// ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
// Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); | |
// bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); | |
// byte[] bytes = baos.toByteArray(); | |
ByteArrayBody bab = new ByteArrayBody(bytes, "pic.png"); | |
HttpEntity entity = MultipartEntityBuilder.create() | |
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE) | |
.setBoundary(boundary) | |
.addPart("img", bab) | |
.build(); | |
httpPost.setEntity(entity); | |
HttpResponse response = httpclient.execute(httpPost); | |
Log.d("Http Post Response:", EntityUtils.toString(response.getEntity())); | |
} catch (ClientProtocolException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
/** | |
* Take a picture. | |
*/ | |
private void takePicture() { | |
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | |
startActivityForResult(intent, TAKE_PICTURE_REQUEST); | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
if (requestCode == TAKE_PICTURE_REQUEST && resultCode == RESULT_OK) { | |
String thumbnailPath = data.getStringExtra(Intents.EXTRA_THUMBNAIL_FILE_PATH); | |
String picturePath = data.getStringExtra(Intents.EXTRA_PICTURE_FILE_PATH); | |
processPictureWhenReady(picturePath); | |
// TODO: Show the thumbnail to the user while the full picture is being | |
// processed. | |
} | |
super.onActivityResult(requestCode, resultCode, data); | |
} | |
private void processPictureWhenReady(final String picturePath) { | |
final File pictureFile = new File(picturePath); | |
if (pictureFile.exists()) { | |
// The picture is ready; process it. | |
//192.168.1.160/upload/index.php | |
makePost(pictureFile); | |
} else { | |
// The file does not exist yet. Before starting the file observer, you | |
// can update your UI to let the user know that the application is | |
// waiting for the picture (for example, by displaying the thumbnail | |
// image and a progress indicator). | |
final File parentDirectory = pictureFile.getParentFile(); | |
FileObserver observer = new FileObserver(parentDirectory.getPath(), | |
FileObserver.CLOSE_WRITE | FileObserver.MOVED_TO) { | |
// Protect against additional pending events after CLOSE_WRITE | |
// or MOVED_TO is handled. | |
private boolean isFileWritten; | |
@Override | |
public void onEvent(int event, String path) { | |
if (!isFileWritten) { | |
// For safety, make sure that the file that was created in | |
// the directory is actually the one that we're expecting. | |
File affectedFile = new File(parentDirectory, path); | |
isFileWritten = affectedFile.equals(pictureFile); | |
if (isFileWritten) { | |
stopWatching(); | |
// Now that the file is ready, recursively call | |
// processPictureWhenReady again (on the UI thread). | |
runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
processPictureWhenReady(picturePath); | |
} | |
}); | |
} | |
} | |
} | |
}; | |
observer.startWatching(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment