DisplayUtils.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Andy Scherzinger
  5. * @author Bartek Przybylski
  6. * @author David A. Velasco
  7. * Copyright (C) 2011 Bartek Przybylski
  8. * Copyright (C) 2015 ownCloud Inc.
  9. * Copyright (C) 2016 Andy Scherzinger
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  13. * License as published by the Free Software Foundation; either
  14. * version 3 of the License, or any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public
  22. * License along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. package com.owncloud.android.utils;
  25. import android.accounts.Account;
  26. import android.annotation.TargetApi;
  27. import android.app.Activity;
  28. import android.content.Context;
  29. import android.content.Intent;
  30. import android.content.res.Resources;
  31. import android.graphics.Bitmap;
  32. import android.graphics.Color;
  33. import android.graphics.Point;
  34. import android.graphics.drawable.Drawable;
  35. import android.graphics.drawable.PictureDrawable;
  36. import android.net.Uri;
  37. import android.os.Build;
  38. import android.support.annotation.NonNull;
  39. import android.support.design.widget.BottomNavigationView;
  40. import android.text.Spannable;
  41. import android.text.SpannableStringBuilder;
  42. import android.text.format.DateUtils;
  43. import android.text.style.StyleSpan;
  44. import android.view.Menu;
  45. import android.view.MenuItem;
  46. import android.view.View;
  47. import com.bumptech.glide.GenericRequestBuilder;
  48. import com.bumptech.glide.Glide;
  49. import com.bumptech.glide.load.engine.DiskCacheStrategy;
  50. import com.bumptech.glide.load.model.StreamEncoder;
  51. import com.bumptech.glide.load.resource.file.FileToStreamDecoder;
  52. import com.bumptech.glide.request.target.SimpleTarget;
  53. import com.caverock.androidsvg.SVG;
  54. import com.owncloud.android.MainApp;
  55. import com.owncloud.android.R;
  56. import com.owncloud.android.authentication.AccountUtils;
  57. import com.owncloud.android.datamodel.FileDataStorageManager;
  58. import com.owncloud.android.datamodel.OCFile;
  59. import com.owncloud.android.datamodel.ThumbnailsCacheManager;
  60. import com.owncloud.android.lib.common.OwnCloudAccount;
  61. import com.owncloud.android.lib.common.utils.Log_OC;
  62. import com.owncloud.android.lib.resources.files.SearchOperation;
  63. import com.owncloud.android.ui.TextDrawable;
  64. import com.owncloud.android.ui.activity.FileDisplayActivity;
  65. import com.owncloud.android.ui.events.MenuItemClickEvent;
  66. import com.owncloud.android.ui.events.SearchEvent;
  67. import com.owncloud.android.ui.fragment.OCFileListFragment;
  68. import com.owncloud.android.utils.svg.SvgDecoder;
  69. import com.owncloud.android.utils.svg.SvgDrawableTranscoder;
  70. import org.greenrobot.eventbus.EventBus;
  71. import org.parceler.Parcels;
  72. import java.io.BufferedReader;
  73. import java.io.IOException;
  74. import java.io.InputStream;
  75. import java.io.InputStreamReader;
  76. import java.math.BigDecimal;
  77. import java.net.IDN;
  78. import java.text.DateFormat;
  79. import java.util.Collection;
  80. import java.util.Date;
  81. import java.util.HashMap;
  82. import java.util.HashSet;
  83. import java.util.Map;
  84. import java.util.Set;
  85. /**
  86. * A helper class for UI/display related operations.
  87. */
  88. public class DisplayUtils {
  89. private static final String TAG = DisplayUtils.class.getSimpleName();
  90. private static final String[] sizeSuffixes = {"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
  91. private static final int[] sizeScales = {0, 0, 1, 1, 1, 2, 2, 2, 2};
  92. private static final int RELATIVE_THRESHOLD_WARNING = 90;
  93. private static final int RELATIVE_THRESHOLD_CRITICAL = 95;
  94. private static final String MIME_TYPE_UNKNOWN = "Unknown type";
  95. private static final String HTTP_PROTOCOLL = "http://";
  96. private static final String HTTPS_PROTOCOLL = "https://";
  97. private static final String TWITTER_HANDLE_PREFIX = "@";
  98. private static Map<String, String> mimeType2HumanReadable;
  99. static {
  100. mimeType2HumanReadable = new HashMap<>();
  101. // images
  102. mimeType2HumanReadable.put("image/jpeg", "JPEG image");
  103. mimeType2HumanReadable.put("image/jpg", "JPEG image");
  104. mimeType2HumanReadable.put("image/png", "PNG image");
  105. mimeType2HumanReadable.put("image/bmp", "Bitmap image");
  106. mimeType2HumanReadable.put("image/gif", "GIF image");
  107. mimeType2HumanReadable.put("image/svg+xml", "JPEG image");
  108. mimeType2HumanReadable.put("image/tiff", "TIFF image");
  109. // music
  110. mimeType2HumanReadable.put("audio/mpeg", "MP3 music file");
  111. mimeType2HumanReadable.put("application/ogg", "OGG music file");
  112. }
  113. /**
  114. * Converts the file size in bytes to human readable output.
  115. * <ul>
  116. * <li>appends a size suffix, e.g. B, KB, MB etc.</li>
  117. * <li>rounds the size based on the suffix to 0,1 or 2 decimals</li>
  118. * </ul>
  119. *
  120. * @param bytes Input file size
  121. * @return something readable like "12 MB", {@link com.owncloud.android.R.string#common_pending} for negative
  122. * byte values
  123. */
  124. public static String bytesToHumanReadable(long bytes) {
  125. if (bytes < 0) {
  126. return MainApp.getAppContext().getString(R.string.common_pending);
  127. } else {
  128. double result = bytes;
  129. int suffixIndex = 0;
  130. while (result > 1024 && suffixIndex < sizeSuffixes.length) {
  131. result /= 1024.;
  132. suffixIndex++;
  133. }
  134. return new BigDecimal(String.valueOf(result)).setScale(
  135. sizeScales[suffixIndex], BigDecimal.ROUND_HALF_UP) + " " + sizeSuffixes[suffixIndex];
  136. }
  137. }
  138. /**
  139. * Converts MIME types like "image/jpg" to more end user friendly output
  140. * like "JPG image".
  141. *
  142. * @param mimetype MIME type to convert
  143. * @return A human friendly version of the MIME type, {@link #MIME_TYPE_UNKNOWN} if it can't be converted
  144. */
  145. public static String convertMIMEtoPrettyPrint(String mimetype) {
  146. if (mimeType2HumanReadable.containsKey(mimetype)) {
  147. return mimeType2HumanReadable.get(mimetype);
  148. }
  149. if (mimetype.split("/").length >= 2) {
  150. return mimetype.split("/")[1].toUpperCase() + " file";
  151. }
  152. return MIME_TYPE_UNKNOWN;
  153. }
  154. /**
  155. * Converts Unix time to human readable format
  156. *
  157. * @param milliseconds that have passed since 01/01/1970
  158. * @return The human readable time for the users locale
  159. */
  160. public static String unixTimeToHumanReadable(long milliseconds) {
  161. Date date = new Date(milliseconds);
  162. DateFormat df = DateFormat.getDateTimeInstance();
  163. return df.format(date);
  164. }
  165. /**
  166. * beautifies a given URL by removing any http/https protocol prefix.
  167. *
  168. * @param url to be beautified url
  169. * @return beautified url
  170. */
  171. public static String beautifyURL(String url) {
  172. if (url == null) {
  173. return "";
  174. }
  175. if (url.length() >= 7 && url.substring(0, 7).equalsIgnoreCase(HTTP_PROTOCOLL)) {
  176. return url.substring(HTTP_PROTOCOLL.length());
  177. }
  178. if (url.length() >= 8 && url.substring(0, 8).equalsIgnoreCase(HTTPS_PROTOCOLL)) {
  179. return url.substring(HTTPS_PROTOCOLL.length());
  180. }
  181. return url;
  182. }
  183. /**
  184. * beautifies a given twitter handle by prefixing it with an @ in case it is missing.
  185. *
  186. * @param handle to be beautified twitter handle
  187. * @return beautified twitter handle
  188. */
  189. public static String beautifyTwitterHandle(String handle) {
  190. if (handle == null) {
  191. return "";
  192. }
  193. if (handle.startsWith(TWITTER_HANDLE_PREFIX)) {
  194. return handle;
  195. } else {
  196. return TWITTER_HANDLE_PREFIX + handle;
  197. }
  198. }
  199. /**
  200. * Converts an internationalized domain name (IDN) in an URL to and from ASCII/Unicode.
  201. *
  202. * @param url the URL where the domain name should be converted
  203. * @param toASCII if true converts from Unicode to ASCII, if false converts from ASCII to Unicode
  204. * @return the URL containing the converted domain name
  205. */
  206. @TargetApi(Build.VERSION_CODES.GINGERBREAD)
  207. public static String convertIdn(String url, boolean toASCII) {
  208. String urlNoDots = url;
  209. String dots = "";
  210. while (urlNoDots.startsWith(".")) {
  211. urlNoDots = url.substring(1);
  212. dots = dots + ".";
  213. }
  214. // Find host name after '//' or '@'
  215. int hostStart = 0;
  216. if (urlNoDots.contains("//")) {
  217. hostStart = url.indexOf("//") + "//".length();
  218. } else if (url.contains("@")) {
  219. hostStart = url.indexOf('@') + "@".length();
  220. }
  221. int hostEnd = url.substring(hostStart).indexOf("/");
  222. // Handle URL which doesn't have a path (path is implicitly '/')
  223. hostEnd = (hostEnd == -1 ? urlNoDots.length() : hostStart + hostEnd);
  224. String host = urlNoDots.substring(hostStart, hostEnd);
  225. host = (toASCII ? IDN.toASCII(host) : IDN.toUnicode(host));
  226. return dots + urlNoDots.substring(0, hostStart) + host + urlNoDots.substring(hostEnd);
  227. }
  228. /**
  229. * creates the display string for an account.
  230. *
  231. * @param context the actual activity
  232. * @param savedAccount the actual, saved account
  233. * @param accountName the account name
  234. * @param fallbackString String to be used in case of an error
  235. * @return the display string for the given account data
  236. */
  237. public static String getAccountNameDisplayText(Context context, Account savedAccount, String accountName, String
  238. fallbackString) {
  239. try {
  240. return new OwnCloudAccount(savedAccount, context).getDisplayName()
  241. + " @ "
  242. + convertIdn(accountName.substring(accountName.lastIndexOf('@') + 1), false);
  243. } catch (Exception e) {
  244. Log_OC.w(TAG, "Couldn't get display name for account, using old style");
  245. return fallbackString;
  246. }
  247. }
  248. /**
  249. * converts an array of accounts into a set of account names.
  250. *
  251. * @param accountList the account array
  252. * @return set of account names
  253. */
  254. public static Set<String> toAccountNameSet(Collection<Account> accountList) {
  255. Set<String> actualAccounts = new HashSet<>(accountList.size());
  256. for (Account account : accountList) {
  257. actualAccounts.add(account.name);
  258. }
  259. return actualAccounts;
  260. }
  261. /**
  262. * calculates the relative time string based on the given modificaion timestamp.
  263. *
  264. * @param context the app's context
  265. * @param modificationTimestamp the UNIX timestamp of the file modification time.
  266. * @return a relative time string
  267. */
  268. public static CharSequence getRelativeTimestamp(Context context, long modificationTimestamp) {
  269. return getRelativeDateTimeString(context, modificationTimestamp, DateUtils.SECOND_IN_MILLIS,
  270. DateUtils.WEEK_IN_MILLIS, 0);
  271. }
  272. /**
  273. * determines the info level color based on certain thresholds
  274. * {@link #RELATIVE_THRESHOLD_WARNING} and {@link #RELATIVE_THRESHOLD_CRITICAL}.
  275. *
  276. * @param context the app's context
  277. * @param relative relative value for which the info level color should be looked up
  278. * @return info level color
  279. */
  280. public static int getRelativeInfoColor(Context context, int relative) {
  281. if (relative < RELATIVE_THRESHOLD_WARNING) {
  282. if (ThemeUtils.colorToHexString(ThemeUtils.primaryColor()).equalsIgnoreCase(
  283. ThemeUtils.colorToHexString(context.getResources().getColor(R.color.primary)))) {
  284. return context.getResources().getColor(R.color.infolevel_info);
  285. } else {
  286. return Color.GRAY;
  287. }
  288. } else if (relative >= RELATIVE_THRESHOLD_WARNING && relative < RELATIVE_THRESHOLD_CRITICAL) {
  289. return context.getResources().getColor(R.color.infolevel_warning);
  290. } else {
  291. return context.getResources().getColor(R.color.infolevel_critical);
  292. }
  293. }
  294. public static CharSequence getRelativeDateTimeString(
  295. Context c, long time, long minResolution, long transitionResolution, int flags) {
  296. CharSequence dateString = "";
  297. // in Future
  298. if (time > System.currentTimeMillis()) {
  299. return DisplayUtils.unixTimeToHumanReadable(time);
  300. }
  301. // < 60 seconds -> seconds ago
  302. else if ((System.currentTimeMillis() - time) < 60 * 1000) {
  303. return c.getString(R.string.file_list_seconds_ago);
  304. } else {
  305. dateString = DateUtils.getRelativeDateTimeString(c, time, minResolution, transitionResolution, flags);
  306. }
  307. String[] parts = dateString.toString().split(",");
  308. if (parts.length == 2) {
  309. if (parts[1].contains(":") && !parts[0].contains(":")) {
  310. return parts[0];
  311. } else if (parts[0].contains(":") && !parts[1].contains(":")) {
  312. return parts[1];
  313. }
  314. }
  315. //dateString contains unexpected format. fallback: use relative date time string from android api as is.
  316. return dateString.toString();
  317. }
  318. /**
  319. * Update the passed path removing the last "/" if it is not the root folder.
  320. *
  321. * @param path the path to be trimmed
  322. */
  323. public static String getPathWithoutLastSlash(String path) {
  324. // Remove last slash from path
  325. if (path.length() > 1 && path.charAt(path.length() - 1) == OCFile.PATH_SEPARATOR.charAt(0)) {
  326. return path.substring(0, path.length() - 1);
  327. }
  328. return path;
  329. }
  330. /**
  331. * Gets the screen size in pixels.
  332. *
  333. * @param caller Activity calling; needed to get access to the {@link android.view.WindowManager}
  334. * @return Size in pixels of the screen, or default {@link Point} if caller is null
  335. */
  336. public static Point getScreenSize(Activity caller) {
  337. Point size = new Point();
  338. if (caller != null) {
  339. caller.getWindowManager().getDefaultDisplay().getSize(size);
  340. }
  341. return size;
  342. }
  343. /**
  344. * styling of given spanText within a given text.
  345. *
  346. * @param text the non styled complete text
  347. * @param spanText the to be styled text
  348. * @param style the style to be applied
  349. */
  350. public static SpannableStringBuilder createTextWithSpan(String text, String spanText, StyleSpan style) {
  351. SpannableStringBuilder sb = new SpannableStringBuilder(text);
  352. int start = text.lastIndexOf(spanText);
  353. int end = start + spanText.length();
  354. sb.setSpan(style, start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
  355. return sb;
  356. }
  357. public interface AvatarGenerationListener {
  358. void avatarGenerated(Drawable avatarDrawable, Object callContext);
  359. boolean shouldCallGeneratedCallback(String tag, Object callContext);
  360. }
  361. /**
  362. * fetches and sets the avatar of the current account in the drawer in case the drawer is available.
  363. *
  364. * @param account the account to be set in the drawer
  365. * @param avatarRadius the avatar radius
  366. * @param resources reference for density information
  367. * @param storageManager reference for caching purposes
  368. */
  369. public static void setAvatar(Account account, AvatarGenerationListener listener, float avatarRadius, Resources resources,
  370. FileDataStorageManager storageManager, Object callContext) {
  371. if (account != null) {
  372. if (callContext instanceof View) {
  373. ((View) callContext).setContentDescription(account.name);
  374. }
  375. // Thumbnail in Cache?
  376. Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache("a_" + account.name);
  377. if (thumbnail != null) {
  378. listener.avatarGenerated(
  379. BitmapUtils.bitmapToCircularBitmapDrawable(MainApp.getAppContext().getResources(), thumbnail),
  380. callContext);
  381. } else {
  382. // generate new avatar
  383. if (ThumbnailsCacheManager.cancelPotentialAvatarWork(account.name, callContext)) {
  384. final ThumbnailsCacheManager.AvatarGenerationTask task =
  385. new ThumbnailsCacheManager.AvatarGenerationTask(listener, callContext, storageManager, account);
  386. if (thumbnail == null) {
  387. try {
  388. listener.avatarGenerated(TextDrawable.createAvatar(account.name, avatarRadius), callContext);
  389. } catch (Exception e) {
  390. Log_OC.e(TAG, "Error calculating RGB value for active account icon.", e);
  391. listener.avatarGenerated(resources.getDrawable(R.drawable.ic_account_circle), callContext);
  392. }
  393. } else {
  394. final ThumbnailsCacheManager.AsyncAvatarDrawable asyncDrawable =
  395. new ThumbnailsCacheManager.AsyncAvatarDrawable(resources, thumbnail, task);
  396. listener.avatarGenerated(BitmapUtils.bitmapToCircularBitmapDrawable(
  397. resources, asyncDrawable.getBitmap()), callContext);
  398. }
  399. task.execute(account.name);
  400. }
  401. }
  402. }
  403. }
  404. public static void downloadIcon(Context context, String iconUrl, SimpleTarget imageView, int placeholder,
  405. int width, int height) {
  406. try {
  407. if (iconUrl.endsWith(".svg")) {
  408. downloadSVGIcon(context, iconUrl, imageView, placeholder, width, height);
  409. } else {
  410. downloadPNGIcon(context, iconUrl, imageView, placeholder);
  411. }
  412. } catch (Exception e) {
  413. Log_OC.d(TAG, "not setting image as activity is destroyed");
  414. }
  415. }
  416. private static void downloadPNGIcon(Context context, String iconUrl, SimpleTarget imageView, int placeholder) {
  417. Glide
  418. .with(context)
  419. .load(iconUrl)
  420. .centerCrop()
  421. .placeholder(placeholder)
  422. .error(placeholder)
  423. .crossFade()
  424. .into(imageView);
  425. }
  426. private static void downloadSVGIcon(Context context, String iconUrl, SimpleTarget imageView, int placeholder,
  427. int width, int height) {
  428. GenericRequestBuilder<Uri, InputStream, SVG, PictureDrawable> requestBuilder = Glide.with(context)
  429. .using(Glide.buildStreamModelLoader(Uri.class, context), InputStream.class)
  430. .from(Uri.class)
  431. .as(SVG.class)
  432. .transcode(new SvgDrawableTranscoder(), PictureDrawable.class)
  433. .sourceEncoder(new StreamEncoder())
  434. .cacheDecoder(new FileToStreamDecoder<>(new SvgDecoder(height, width)))
  435. .decoder(new SvgDecoder(height, width))
  436. .placeholder(placeholder)
  437. .error(placeholder)
  438. .animate(android.R.anim.fade_in);
  439. Uri uri = Uri.parse(iconUrl);
  440. requestBuilder
  441. .diskCacheStrategy(DiskCacheStrategy.SOURCE)
  442. .load(uri)
  443. .into(imageView);
  444. }
  445. public static void setupBottomBar(BottomNavigationView view, Resources resources, final Activity activity,
  446. int checkedMenuItem) {
  447. Menu menu = view.getMenu();
  448. Account account = AccountUtils.getCurrentOwnCloudAccount(MainApp.getAppContext());
  449. boolean searchSupported = AccountUtils.hasSearchSupport(account);
  450. if (!searchSupported) {
  451. menu.removeItem(R.id.nav_bar_favorites);
  452. menu.removeItem(R.id.nav_bar_photos);
  453. }
  454. if (resources.getBoolean(R.bool.use_home)) {
  455. menu.findItem(R.id.nav_bar_files).setTitle(resources.
  456. getString(R.string.drawer_item_home));
  457. menu.findItem(R.id.nav_bar_files).setIcon(R.drawable.ic_home);
  458. }
  459. setBottomBarItem(view, checkedMenuItem);
  460. view.setOnNavigationItemSelectedListener(
  461. new BottomNavigationView.OnNavigationItemSelectedListener() {
  462. @Override
  463. public boolean onNavigationItemSelected(@NonNull MenuItem item) {
  464. switch (item.getItemId()) {
  465. case R.id.nav_bar_files:
  466. EventBus.getDefault().post(new MenuItemClickEvent(item));
  467. if (activity != null) {
  468. activity.invalidateOptionsMenu();
  469. }
  470. break;
  471. case R.id.nav_bar_favorites:
  472. SearchEvent favoritesEvent = new SearchEvent("",
  473. SearchOperation.SearchType.FAVORITE_SEARCH,
  474. SearchEvent.UnsetType.UNSET_DRAWER);
  475. switchToSearchFragment(activity, favoritesEvent);
  476. break;
  477. case R.id.nav_bar_photos:
  478. SearchEvent photosEvent = new SearchEvent("image/%",
  479. SearchOperation.SearchType.CONTENT_TYPE_SEARCH,
  480. SearchEvent.UnsetType.UNSET_DRAWER);
  481. switchToSearchFragment(activity, photosEvent);
  482. break;
  483. case R.id.nav_bar_settings:
  484. EventBus.getDefault().post(new MenuItemClickEvent(item));
  485. break;
  486. default:
  487. break;
  488. }
  489. return true;
  490. }
  491. });
  492. }
  493. public static void setBottomBarItem(BottomNavigationView view, int checkedMenuItem) {
  494. Menu menu = view.getMenu();
  495. for (int i = 0; i < menu.size(); i++) {
  496. menu.getItem(i).setChecked(false);
  497. }
  498. if (checkedMenuItem != -1) {
  499. menu.findItem(checkedMenuItem).setChecked(true);
  500. }
  501. }
  502. private static void switchToSearchFragment(Activity activity, SearchEvent event) {
  503. if (activity instanceof FileDisplayActivity) {
  504. EventBus.getDefault().post(event);
  505. } else {
  506. Intent recentlyAddedIntent = new Intent(activity.getBaseContext(), FileDisplayActivity.class);
  507. recentlyAddedIntent.putExtra(OCFileListFragment.SEARCH_EVENT, Parcels.wrap(event));
  508. recentlyAddedIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  509. activity.startActivity(recentlyAddedIntent);
  510. }
  511. }
  512. /**
  513. * Get String data from a InputStream
  514. *
  515. * @param inputStream The File InputStream
  516. */
  517. public static String getData(InputStream inputStream) {
  518. BufferedReader buffreader = new BufferedReader(new InputStreamReader(inputStream));
  519. String line;
  520. StringBuilder text = new StringBuilder();
  521. try {
  522. while ((line = buffreader.readLine()) != null) {
  523. text.append(line);
  524. text.append('\n');
  525. }
  526. } catch (IOException e) {
  527. Log_OC.e(TAG, e.getMessage());
  528. }
  529. return text.toString();
  530. }
  531. }