1 package com.introlab.rtabmap;
4 import java.io.IOException;
7 import android.app.Activity;
8 import android.app.AlertDialog;
9 import android.app.Dialog;
10 import android.app.ProgressDialog;
11 import android.content.Context;
12 import android.content.DialogInterface;
13 import android.content.Intent;
14 import android.content.SharedPreferences;
15 import android.net.ConnectivityManager;
16 import android.net.NetworkInfo;
17 import android.os.AsyncTask;
18 import android.os.Bundle;
19 import android.preference.PreferenceManager;
20 import android.text.Editable;
21 import android.text.SpannableString;
22 import android.text.TextWatcher;
23 import android.text.method.LinkMovementMethod;
24 import android.text.util.Linkify;
25 import android.util.Log;
26 import android.view.View;
27 import android.view.View.OnClickListener;
28 import android.webkit.WebView;
29 import android.webkit.WebViewClient;
30 import android.widget.Button;
31 import android.widget.CheckBox;
32 import android.widget.EditText;
33 import android.widget.TextView;
34 import android.widget.Toast;
38 private static final String
AUTHORIZE_PATH =
"https://sketchfab.com/oauth2/authorize";
39 private static final String
CLIENT_ID =
"RXrIJYAwlTELpySsyM8TrK9r3kOGQ5Qjj9VVDIfV";
40 private static final String
REDIRECT_URI =
"https://introlab.github.io/rtabmap/oauth2_redirect";
42 ProgressDialog mProgressDialog;
50 EditText mDescription;
58 protected void onCreate(Bundle savedInstanceState) {
59 super.onCreate(savedInstanceState);
60 setContentView(R.layout.activity_sketchfab);
62 mFilename = (EditText)findViewById(R.id.editText_filename);
63 mDescription = (EditText)findViewById(R.id.editText_description);
64 mTags = (EditText)findViewById(R.id.editText_tags);
65 mDraft = (CheckBox)findViewById(R.id.checkBox_draft);
66 mButtonOk = (Button)findViewById(R.id.button_ok);
68 mProgressDialog =
new ProgressDialog(
this);
69 mProgressDialog.setCanceledOnTouchOutside(
false);
75 SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(
this);
76 String tags = sharedPref.getString(getString(R.string.pref_key_tags), getString(R.string.pref_default_tags));
79 tags = getString(R.string.pref_default_tags);
83 mButtonOk.setEnabled(mFilename.getText().toString().length()>0);
84 mButtonOk.setOnClickListener(
this);
86 mFilename.addTextChangedListener(
new TextWatcher() {
89 public void afterTextChanged(Editable s) {}
92 public void beforeTextChanged(CharSequence s,
int start,
93 int count,
int after) {
97 public void onTextChanged(CharSequence s,
int start,
98 int before,
int count) {
99 mButtonOk.setEnabled(s.length() != 0);
103 mFilename.setSelectAllOnFocus(
true);
104 mFilename.requestFocus();
121 if(!mTags.getText().toString().isEmpty())
123 SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(
this);
124 SharedPreferences.Editor editor = sharedPref.edit();
125 editor.putString(getString(R.string.pref_key_tags), mTags.getText().toString());
134 ConnectivityManager connectivityManager
135 = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
136 NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
137 return activeNetworkInfo != null && activeNetworkInfo.isConnected();
145 new AlertDialog.Builder(
this)
146 .setTitle(
"Sharing to Sketchfab...")
147 .setMessage(
"Network is not available. Make sure you have internet before continuing.")
148 .setPositiveButton(
"Try Again",
new DialogInterface.OnClickListener() {
149 public void onClick(DialogInterface dialog,
int which) {
153 .setNeutralButton(
"Abort",
new DialogInterface.OnClickListener() {
154 public void onClick(DialogInterface dialog,
int which) {
162 if(mAuthToken == null)
167 mAuthDialog =
new Dialog(
this);
168 mAuthDialog.setContentView(R.layout.auth_dialog);
169 web = (WebView)mAuthDialog.findViewById(R.id.webv);
171 web.getSettings().setJavaScriptEnabled(
true);
172 String auth_url = AUTHORIZE_PATH+
"?redirect_uri="+REDIRECT_URI+
"&response_type=token&client_id="+
CLIENT_ID;
174 web.setWebViewClient(
new WebViewClient() {
176 boolean authComplete =
false;
179 public void onPageFinished(WebView view, String url) {
180 super.onPageFinished(view, url);
183 if(url.contains(
"error=access_denied")){
186 Toast.makeText(getApplicationContext(),
"Error Occured", Toast.LENGTH_SHORT).show();
187 mAuthDialog.dismiss();
189 else if (url.startsWith(REDIRECT_URI) && url.contains(
"access_token") && authComplete !=
true) {
191 String[] sArray = url.split(
"access_token=");
192 mAuthToken = (sArray[1].split(
"&token_type=Bearer"))[0];
195 mAuthDialog.dismiss();
202 mAuthDialog.setTitle(
"Authorize RTAB-Map");
203 mAuthDialog.setCancelable(
true);
204 web.loadUrl(auth_url);
214 mProgressDialog.setTitle(
"Upload to Sketchfab");
215 mProgressDialog.setMessage(String.format(
"Compressing the files..."));
216 mProgressDialog.show();
218 Thread workingThread =
new Thread(
new Runnable() {
226 for(
int i=0; i<fileNames.length; ++i)
243 String[] files =
new String[0];
247 if(fileNames.length > 0)
249 files =
new String[fileNames.length];
250 for(
int i=0; i<fileNames.length; ++i)
262 final String[] filesToZip = files;
263 final String zipOutput = mWorkingDirectory+fileName+
".zip";
264 Util.
zip(filesToZip, zipOutput);
265 runOnUiThread(
new Runnable() {
267 mProgressDialog.dismiss();
269 File
f =
new File(zipOutput);
272 AlertDialog.Builder builder =
new AlertDialog.Builder(
getActivity());
273 builder.setTitle(
"File(s) compressed and ready to upload!");
275 final int fileSizeMB = (int)f.length()/(1024 * 1024);
276 final int fileSizeKB = (int)f.length()/(1024);
280 builder.setMessage(String.format(
"Total size to upload = %d KB. Do you want to continue?\n\n", fileSizeKB));
285 builder.setMessage(String.format(
"Total size to upload = %d MB. %sDo you want to continue?\n\n" 286 +
"Tip: To reduce the model size, you can also look at the Settings->Exporting options.", fileSizeMB,
287 fileSizeMB>=50?
"Note that for size over 50 MB, a Sketchfab PRO account is required, otherwise the upload may fail. ":
""));
291 builder.setPositiveButton(
"Yes",
new DialogInterface.OnClickListener() {
292 public void onClick(DialogInterface dialog,
int which) {
293 mProgressDialog.setTitle(
"Upload to Sketchfab");
296 mProgressDialog.setMessage(String.format(
"Uploading model \"%s\" (%d KB) to Sketchfab...", fileName, fileSizeKB));
300 mProgressDialog.setMessage(String.format(
"Uploading model \"%s\" (%d MB) to Sketchfab...", fileName, fileSizeMB));
302 mProgressDialog.show();
306 builder.setNegativeButton(
"No",
new DialogInterface.OnClickListener() {
307 public void onClick(DialogInterface dialog,
int which) {
318 runOnUiThread(
new Runnable() {
320 mProgressDialog.dismiss();
321 Toast.makeText(
getActivity(), String.format(
"Failed writing files!"), Toast.LENGTH_LONG).show();
326 catch(IOException ex) {
332 workingThread.start();
338 String mModelFilePath;
349 mProgressDialog.dismiss();
352 if(mModelFilePath!= null)
354 File
f =
new File(mModelFilePath);
358 final SpannableString s =
new SpannableString(
359 "Model \"" + mFileName +
"\" is now processing on Sketchfab! You can click " 360 +
"on the link below to see it on Sketchfab.\n\nhttps://sketchfab.com/models/"+mModelUri);
361 Linkify.addLinks(s, Linkify.WEB_URLS);
362 final AlertDialog
d =
new AlertDialog.Builder(
getActivity())
363 .setTitle(
"Upload finished!")
364 .setCancelable(
false)
366 .setPositiveButton(
"Close",
new DialogInterface.OnClickListener() {
367 public void onClick(DialogInterface dialog,
int which) {
368 Intent resultIntent =
new Intent();
370 setResult(Activity.RESULT_OK, resultIntent);
375 ((TextView)d.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
379 Toast.makeText(getApplicationContext(), String.format(
"Upload failed! Error=\"%s\"", error), Toast.LENGTH_SHORT).show();
385 String charset =
"UTF-8";
386 File uploadFile =
new File(files[0]);
387 mFileName = files[1];
388 String requestURL =
"https://api.sketchfab.com/v3/models";
396 multipart.
addFormField(
"description", mDescription.getText().toString());
397 multipart.
addFormField(
"tags", mTags.getText().toString());
399 multipart.
addFormField(
"isPublished", mDraft.isChecked()?
"false":
"true");
403 List<String> response = multipart.
finish();
407 for (String line : response) {
410 if(line.contains(
"\"uid\":\""))
412 String[] sArray = line.split(
"\"uid\":\"");
413 mModelUri = (sArray[1].split(
"\""))[0];
414 mModelFilePath = files[0];
446 }
catch (IOException ex) {
448 error = ex.getMessage();
static final String RTABMAP_EXPORT_DIR
static boolean DISABLE_LOG
boolean isNetworkAvailable()
static final String RTABMAP_FILENAME_KEY
static final String CLIENT_ID
static void zip(String file, String zipFile)
void authorizeAndPublish(final String fileName)
void addFilePart(String fieldName, File uploadFile)
SketchfabActivity getActivity()
static final String RTABMAP_WORKING_DIR_KEY
Void doInBackground(String...files)
static String[] loadFileList(String directory, final boolean databasesOnly)
static final String REDIRECT_URI
static final String AUTHORIZE_PATH
static native boolean writeExportedMesh(String directory, String name)
void onCreate(Bundle savedInstanceState)
static final String RTABMAP_AUTH_TOKEN_KEY
static final String RTABMAP_TMP_DIR
void onPostExecute(Void result)
void run(ClassLoader *loader)
void zipAndPublish(final String fileName)
void addFormField(String name, String value)
static final String RTABMAP_TMP_FILENAME