Skip to content

Commit

Permalink
Merge branch 'fix_file_message_name' into branch_1.6.1
Browse files Browse the repository at this point in the history
  • Loading branch information
skykelsey committed Sep 24, 2014
2 parents 6e7ce61 + af4b104 commit bdf9373
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ private static ApptentiveHttpResponse performMultipartFilePost(Context context,

// Write file attributes.
requestText.append(twoHyphens).append(boundary).append(lineEnd);
requestText.append("Content-Disposition: form-data; name=\"file\"; filename=\"file.png\"").append(lineEnd);
requestText.append(String.format("Content-Disposition: form-data; name=\"file\"; filename=\"%s\"", storedFile.getFileName())).append(lineEnd);
requestText.append("Content-Type: ").append(storedFile.getMimeType()).append(lineEnd);
requestText.append(lineEnd);

Expand Down
16 changes: 12 additions & 4 deletions apptentive/src/com/apptentive/android/sdk/model/FileMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public boolean internalCreateStoredImage(Context context, String uriString) {
storedFile.setId(getStoredFileId());
storedFile.setOriginalUri(uri.toString());
storedFile.setLocalFilePath(localFile.getPath());
storedFile.setMimeType(mimeType);
storedFile.setMimeType("image/jpeg");
FileStore db = ApptentiveDatabase.getInstance(context);
return db.putStoredFile(storedFile);
}
Expand All @@ -152,6 +152,15 @@ public boolean createStoredFile(Context context, String uriString) {
String mimeType = resolver.getType(uri);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String extension = mime.getExtensionFromMimeType(mimeType);

// If we can't get the mime type from the uri, try getting it from the extension.
if (extension == null) {
extension = MimeTypeMap.getFileExtensionFromUrl(uriString);
}
if (mimeType == null && extension != null) {
mimeType = mime.getMimeTypeFromExtension(extension);
}

setFileName(uri.getLastPathSegment() + "." + extension);
setMimeType(mimeType);

Expand Down Expand Up @@ -196,7 +205,7 @@ public boolean createStoredFile(Context context, InputStream is, String mimeType
try {
os = new CountingOutputStream(new BufferedOutputStream(context.openFileOutput(localFile.getPath(), Context.MODE_PRIVATE)));
byte[] buf = new byte[2048];
int count = 0;
int count;
while ((count = is.read(buf, 0, 2048)) != -1) {
os.write(buf, 0, count);
}
Expand All @@ -216,8 +225,7 @@ public boolean createStoredFile(Context context, InputStream is, String mimeType

public StoredFile getStoredFile(Context context) {
FileStore fileStore = ApptentiveDatabase.getInstance(context);
StoredFile storedFile = fileStore.getStoredFile(getStoredFileId());
return storedFile;
return fileStore.getStoredFile(getStoredFileId());
}

public void deleteStoredFile(Context context) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/*
* Copyright (c) 2013, Apptentive, Inc. All Rights Reserved.
* Copyright (c) 2014, Apptentive, Inc. All Rights Reserved.
* Please refer to the LICENSE file for the terms and conditions
* under which redistribution and use of this file is permitted.
*/

package com.apptentive.android.sdk.model;

import android.webkit.MimeTypeMap;

/**
* @author Sky Kelsey
*/
Expand Down Expand Up @@ -55,4 +57,8 @@ public String getApptentiveUri() {
public void setApptentiveUri(String apptentiveUri) {
this.apptentiveUri = apptentiveUri;
}

public String getFileName() {
return String.format("file.%s", MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType));
}
}
3 changes: 1 addition & 2 deletions samples/apptentive-dev/apptentive-dev.iml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
<option name="SOURCE_GEN_TASK_NAME" value="generateDebugSources" />
<option name="ALLOW_USER_CONFIGURATION" value="false" />
<option name="RES_FOLDERS_RELATIVE_PATH" value="file://$MODULE_DIR$/res" />
<option name="ASSETS_FOLDER_RELATIVE_PATH" value="/src/main/assets" />
</configuration>
</facet>
</component>
Expand Down Expand Up @@ -43,7 +42,7 @@
<sourceFolder url="file://$MODULE_DIR$/res" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/main/aidl" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/assets" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/assets" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/jni" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
Expand Down
Binary file added samples/apptentive-dev/assets/test.docx
Binary file not shown.
Binary file added samples/apptentive-dev/assets/test.pdf
Binary file not shown.
Binary file added samples/apptentive-dev/assets/test.pptx
Binary file not shown.
1 change: 1 addition & 0 deletions samples/apptentive-dev/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ android {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
}
23 changes: 22 additions & 1 deletion samples/apptentive-dev/res/layout/message_center.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,28 @@
android:layout_height="fill_parent"
android:onClick="sendAttachmentFile"
android:text="Send"/>
<EditText android:id="@+id/attachment_file_content"
<Spinner android:id="@+id/attachment_file_spinner"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="6"
android:entries="@array/attachment_files"
android:hint="Choose File…"/>
</LinearLayout>
<TextView android:layout_width="fill_parent"
android:layout_height="32dp"
android:gravity="left|bottom"
android:textSize="@dimen/apptentive_text_medium"
android:textColor="@android:color/white"
android:text="Send Text File Attachment"/>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="48dp"
android:layout_marginTop="4dp"
android:orientation="horizontal">
<Button android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:onClick="sendAttachmentTextFile"
android:text="Send"/>
<EditText android:id="@+id/attachment_text_file_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:hint="Text File Contents…"/>
Expand Down
6 changes: 6 additions & 0 deletions samples/apptentive-dev/res/values/spinners.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,10 @@
<item>UpgradeMessage exercise with branding</item>
<item>UpgradeMessage exercise no branding</item>
</string-array>

<string-array name="attachment_files">
<item>test.pptx</item>
<item>test.docx</item>
<item>test.pdf</item>
</string-array>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.webkit.MimeTypeMap;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import com.apptentive.android.dev.util.FileUtil;
import com.apptentive.android.sdk.Apptentive;
import com.apptentive.android.sdk.ApptentiveActivity;
import com.apptentive.android.sdk.Log;
Expand Down Expand Up @@ -64,7 +67,19 @@ public void sendAttachmentImage(View view) {
}

public void sendAttachmentFile(View view) {
EditText text = (EditText) findViewById(R.id.attachment_file_content);
Spinner fileSpinner = (Spinner) findViewById(R.id.attachment_file_spinner);
String fileName = (String) fileSpinner.getSelectedItem();
String uri = FileUtil.createFileAssetUriString(fileName);

String extension = MimeTypeMap.getFileExtensionFromUrl(uri);
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
InputStream fis = FileUtil.openFileAsset(this, fileName);

Apptentive.sendAttachmentFile(this, fis, mimeType);
}

public void sendAttachmentTextFile(View view) {
EditText text = (EditText) findViewById(R.id.attachment_text_file_content);
byte[] bytes = text.getText().toString().getBytes();
Apptentive.sendAttachmentFile(this, bytes, "text/plain");
text.setText("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
import com.apptentive.android.sdk.Log;
import com.apptentive.android.sdk.util.Util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.*;

/**
* @author Sky Kelsey
Expand Down Expand Up @@ -41,4 +39,22 @@ public static String loadTextAssetAsString(Context context, String path) {
}
return null;
}

public static InputStream openFileAsset(Context context, String path) {
AssetManager assetManager = context.getResources().getAssets();
BufferedReader reader = null;
try {
return new BufferedInputStream(assetManager.open(path));
} catch (IOException e) {
Log.e("Error open stream from file \"%s\"", e, path);
} finally {
Util.ensureClosed(reader);
}
return null;
}

public static String createFileAssetUriString(String path) {
return "file:///android_asset/" + path;
}

}

0 comments on commit bdf9373

Please sign in to comment.