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.setCancelable(
false);
70 mProgressDialog.setCanceledOnTouchOutside(
false);
76 SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(
this);
77 String tags = sharedPref.getString(getString(R.string.pref_key_tags), getString(R.string.pref_default_tags));
80 tags = getString(R.string.pref_default_tags);
84 mButtonOk.setEnabled(mFilename.getText().toString().length()>0);
85 mButtonOk.setOnClickListener(
this);
87 mFilename.addTextChangedListener(
new TextWatcher() {
90 public void afterTextChanged(Editable s) {}
93 public void beforeTextChanged(CharSequence s,
int start,
94 int count,
int after) {
98 public void onTextChanged(CharSequence s,
int start,
99 int before,
int count) {
100 mButtonOk.setEnabled(s.length() != 0);
104 mFilename.setSelectAllOnFocus(
true);
105 mFilename.requestFocus();
122 if(!mTags.getText().toString().isEmpty())
124 SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(
this);
125 SharedPreferences.Editor editor = sharedPref.edit();
126 editor.putString(getString(R.string.pref_key_tags), mTags.getText().toString());
135 ConnectivityManager connectivityManager
136 = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
137 NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
138 return activeNetworkInfo != null && activeNetworkInfo.isConnected();
146 AlertDialog ad =
new AlertDialog.Builder(
this)
147 .setCancelable(
false)
148 .setTitle(
"Sharing to Sketchfab...")
149 .setMessage(
"Network is not available. Make sure you have internet before continuing.")
150 .setPositiveButton(
"Try Again",
new DialogInterface.OnClickListener() {
151 public void onClick(DialogInterface dialog,
int which) {
155 .setNeutralButton(
"Abort",
new DialogInterface.OnClickListener() {
156 public void onClick(DialogInterface dialog,
int which) {
160 ad.setCanceledOnTouchOutside(
false);
166 if(mAuthToken == null)
171 mAuthDialog =
new Dialog(
this);
172 mAuthDialog.setCancelable(
true);
173 mAuthDialog.setCanceledOnTouchOutside(
true);
174 mAuthDialog.setContentView(R.layout.auth_dialog);
175 web = (WebView)mAuthDialog.findViewById(R.id.webv);
177 web.getSettings().setJavaScriptEnabled(
true);
178 String auth_url = AUTHORIZE_PATH+
"?redirect_uri="+REDIRECT_URI+
"&response_type=token&client_id="+
CLIENT_ID;
180 web.setWebViewClient(
new WebViewClient() {
182 boolean authComplete =
false;
185 public void onPageFinished(WebView view, String url) {
186 super.onPageFinished(view, url);
189 if(url.contains(
"error=access_denied")){
192 Toast.makeText(getApplicationContext(),
"Error Occured", Toast.LENGTH_SHORT).show();
193 mAuthDialog.dismiss();
195 else if (url.startsWith(REDIRECT_URI) && url.contains(
"access_token") && authComplete !=
true) {
197 String[] sArray = url.split(
"access_token=");
198 mAuthToken = (sArray[1].split(
"&token_type=Bearer"))[0];
201 mAuthDialog.dismiss();
208 mAuthDialog.setTitle(
"Authorize RTAB-Map");
209 web.loadUrl(auth_url);
219 mProgressDialog.setTitle(
"Upload to Sketchfab");
220 mProgressDialog.setMessage(String.format(
"Compressing the files..."));
221 mProgressDialog.setCancelable(
false);
222 mProgressDialog.setCanceledOnTouchOutside(
false);
223 mProgressDialog.show();
225 Thread workingThread =
new Thread(
new Runnable() {
233 for(
int i=0; i<fileNames.length; ++i)
250 String[] files =
new String[0];
254 if(fileNames.length > 0)
256 files =
new String[fileNames.length];
257 for(
int i=0; i<fileNames.length; ++i)
269 final String[] filesToZip = files;
270 final String zipOutput = mWorkingDirectory+fileName+
".zip";
271 Util.
zip(filesToZip, zipOutput);
272 runOnUiThread(
new Runnable() {
274 mProgressDialog.dismiss();
276 File
f =
new File(zipOutput);
279 AlertDialog.Builder builder =
new AlertDialog.Builder(
getActivity());
280 builder.setTitle(
"File(s) compressed and ready to upload!");
282 final int fileSizeMB = (int)f.length()/(1024 * 1024);
283 final int fileSizeKB = (int)f.length()/(1024);
287 builder.setMessage(String.format(
"Total size to upload = %d KB. Do you want to continue?\n\n", fileSizeKB));
292 builder.setMessage(String.format(
"Total size to upload = %d MB. %sDo you want to continue?\n\n" 293 +
"Tip: To reduce the model size, you can also look at the Settings->Exporting options.", fileSizeMB,
294 fileSizeMB>=50?
"Note that for size over 50 MB, a Sketchfab PRO account is required, otherwise the upload may fail. ":
""));
298 builder.setPositiveButton(
"Yes",
new DialogInterface.OnClickListener() {
299 public void onClick(DialogInterface dialog,
int which) {
300 mProgressDialog.setTitle(
"Upload to Sketchfab");
303 mProgressDialog.setMessage(String.format(
"Uploading model \"%s\" (%d KB) to Sketchfab...", fileName, fileSizeKB));
307 mProgressDialog.setMessage(String.format(
"Uploading model \"%s\" (%d MB) to Sketchfab...", fileName, fileSizeMB));
309 mProgressDialog.show();
313 builder.setNegativeButton(
"No",
new DialogInterface.OnClickListener() {
314 public void onClick(DialogInterface dialog,
int which) {
318 AlertDialog ad = builder.create();
319 ad.setCancelable(
false);
320 ad.setCanceledOnTouchOutside(
false);
328 runOnUiThread(
new Runnable() {
330 mProgressDialog.dismiss();
331 Toast.makeText(
getActivity(), String.format(
"Failed writing files!"), Toast.LENGTH_LONG).show();
336 catch(IOException ex) {
342 workingThread.start();
348 String mModelFilePath;
359 mProgressDialog.dismiss();
362 if(mModelFilePath!= null)
364 File
f =
new File(mModelFilePath);
368 final SpannableString s =
new SpannableString(
369 "Model \"" + mFileName +
"\" is now processing on Sketchfab! You can click " 370 +
"on the link below to see it on Sketchfab.\n\nhttps://sketchfab.com/models/"+mModelUri);
371 Linkify.addLinks(s, Linkify.WEB_URLS);
372 final AlertDialog
d =
new AlertDialog.Builder(
getActivity())
373 .setTitle(
"Upload finished!")
374 .setCancelable(
false)
376 .setPositiveButton(
"Close",
new DialogInterface.OnClickListener() {
377 public void onClick(DialogInterface dialog,
int which) {
378 Intent resultIntent =
new Intent();
380 setResult(Activity.RESULT_OK, resultIntent);
384 d.setCanceledOnTouchOutside(
false);
386 ((TextView)d.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
390 Toast.makeText(getApplicationContext(), String.format(
"Upload failed! Error=\"%s\"", error), Toast.LENGTH_SHORT).show();
396 String charset =
"UTF-8";
397 File uploadFile =
new File(files[0]);
398 mFileName = files[1];
399 String requestURL =
"https://api.sketchfab.com/v3/models";
407 multipart.
addFormField(
"description", mDescription.getText().toString());
408 multipart.
addFormField(
"tags", mTags.getText().toString());
410 multipart.
addFormField(
"isPublished", mDraft.isChecked()?
"false":
"true");
414 List<String> response = multipart.
finish();
418 for (String line : response) {
421 if(line.contains(
"\"uid\":\""))
423 String[] sArray = line.split(
"\"uid\":\"");
424 mModelUri = (sArray[1].split(
"\""))[0];
425 mModelFilePath = files[0];
457 }
catch (IOException ex) {
459 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 native boolean writeExportedMesh(long nativeApplication, String directory, String name)
static void zip(String file, String zipFile)
void authorizeAndPublish(final String fileName)
static long nativeApplication
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
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