List of usage examples for android.text TextUtils indexOf
public static int indexOf(CharSequence s, CharSequence needle)
From source file:org.myframe.http.FileRequest.java
public byte[] handleResponse(HttpResponse response) throws IOException, KJHttpException { HttpEntity entity = response.getEntity(); long fileSize = entity.getContentLength(); if (fileSize <= 0) { MLoger.debug("Response doesn't present Content-Length!"); }/*from w w w . ja va2s .co m*/ long downloadedSize = mTemporaryFile.length(); boolean isSupportRange = HttpUtils.isSupportRange(response); if (isSupportRange) { fileSize += downloadedSize; String realRangeValue = HttpUtils.getHeader(response, "Content-Range"); if (!TextUtils.isEmpty(realRangeValue)) { String assumeRangeValue = "bytes " + downloadedSize + "-" + (fileSize - 1); if (TextUtils.indexOf(realRangeValue, assumeRangeValue) == -1) { throw new IllegalStateException("The Content-Range Header is invalid Assume[" + assumeRangeValue + "] vs Real[" + realRangeValue + "], " + "please remove the temporary file [" + mTemporaryFile + "]."); } } } if (fileSize > 0 && mStoreFile.length() == fileSize) { mStoreFile.renameTo(mTemporaryFile); mRequestQueue.getConfig().mDelivery.postDownloadProgress(this, fileSize, fileSize); return null; } RandomAccessFile tmpFileRaf = new RandomAccessFile(mTemporaryFile, "rw"); if (isSupportRange) { tmpFileRaf.seek(downloadedSize); } else { tmpFileRaf.setLength(0); downloadedSize = 0; } try { InputStream in = entity.getContent(); if (HttpUtils.isGzipContent(response) && !(in instanceof GZIPInputStream)) { in = new GZIPInputStream(in); } byte[] buffer = new byte[6 * 1024]; // 6K buffer int offset; while ((offset = in.read(buffer)) != -1) { tmpFileRaf.write(buffer, 0, offset); downloadedSize += offset; mRequestQueue.getConfig().mDelivery.postDownloadProgress(this, fileSize, downloadedSize); if (isCanceled()) { break; } } } finally { try { if (entity != null) entity.consumeContent(); } catch (Exception e) { MLoger.debug("Error occured when calling consumingContent"); } tmpFileRaf.close(); } return null; }
From source file:com.scut.easyfe.network.kjFrame.http.FileRequest.java
public byte[] handleResponse(HttpResponse response) throws IOException, KJHttpException { HttpEntity entity = response.getEntity(); long fileSize = entity.getContentLength(); if (fileSize <= 0) { KJLoger.debug("Response doesn't present Content-Length!"); }/* w w w. j av a 2s .c o m*/ long downloadedSize = mTemporaryFile.length(); boolean isSupportRange = HttpUtils.isSupportRange(response); if (isSupportRange) { fileSize += downloadedSize; String realRangeValue = HttpUtils.getHeader(response, "Content-Range"); if (!TextUtils.isEmpty(realRangeValue)) { String assumeRangeValue = "bytes " + downloadedSize + "-" + (fileSize - 1); if (TextUtils.indexOf(realRangeValue, assumeRangeValue) == -1) { throw new IllegalStateException("The Content-Range Header is invalid Assume[" + assumeRangeValue + "] vs Real[" + realRangeValue + "], " + "please remove the temporary file [" + mTemporaryFile + "]."); } } } if (fileSize > 0 && mStoreFile.length() == fileSize) { mStoreFile.renameTo(mTemporaryFile); mRequestQueue.getConfig().mDelivery.postDownloadProgress(this, fileSize, fileSize); return null; } RandomAccessFile tmpFileRaf = new RandomAccessFile(mTemporaryFile, "rw"); if (isSupportRange) { tmpFileRaf.seek(downloadedSize); } else { tmpFileRaf.setLength(0); downloadedSize = 0; } try { InputStream in = entity.getContent(); if (HttpUtils.isGzipContent(response) && !(in instanceof GZIPInputStream)) { in = new GZIPInputStream(in); } byte[] buffer = new byte[6 * 1024]; // 6K buffer int offset; while ((offset = in.read(buffer)) != -1) { tmpFileRaf.write(buffer, 0, offset); downloadedSize += offset; mRequestQueue.getConfig().mDelivery.postDownloadProgress(this, fileSize, downloadedSize); if (isCanceled()) { break; } } } finally { try { if (entity != null) entity.consumeContent(); } catch (Exception e) { KJLoger.debug("Error occured when calling consumingContent"); } tmpFileRaf.close(); } return null; }
From source file:com.nextgis.maplibui.activity.FormBuilderModifyAttributesActivity.java
@Override protected void fillControls(LinearLayout layout, Bundle savedState) { //TODO: add location control via fragment only defined by user space Bundle extras = getIntent().getExtras(); // null != extras File form = (File) extras.getSerializable(KEY_FORM_PATH); int orientation = getResources().getConfiguration().orientation; boolean isLand = orientation == Configuration.ORIENTATION_LANDSCAPE; try {/*from w w w. j ava 2 s . c o m*/ String formString = FileUtil.readFromFile(form); if (TextUtils.indexOf(formString, "tabs") == -1) { JSONArray elements = new JSONArray(formString); if (elements.length() > 0) { fillTabControls(layout, savedState, elements); } } else { JSONObject jsonFormContents = new JSONObject(formString); JSONArray tabs = jsonFormContents.getJSONArray(JSON_TABS_KEY); for (int i = 0; i < tabs.length(); i++) { JSONObject tab = tabs.getJSONObject(i); JSONArray elements = null; if (isLand && !tab.isNull(JSON_ALBUM_ELEMENTS_KEY)) { elements = tab.getJSONArray(JSON_ALBUM_ELEMENTS_KEY); } if (null == elements) { if (!isLand && !tab.isNull(JSON_PORTRAIT_ELEMENTS_KEY)) { elements = tab.getJSONArray(JSON_PORTRAIT_ELEMENTS_KEY); } } if (null == elements) { if (!tab.isNull(JSON_ALBUM_ELEMENTS_KEY)) { elements = tab.getJSONArray(JSON_ALBUM_ELEMENTS_KEY); } if (!tab.isNull(JSON_PORTRAIT_ELEMENTS_KEY)) { elements = tab.getJSONArray(JSON_ALBUM_ELEMENTS_KEY); } } if (null != elements && elements.length() > 0) { fillTabControls(layout, savedState, elements); } } } } catch (JSONException | IOException e) { e.printStackTrace(); Toast.makeText(this, getString(R.string.error_form_create), Toast.LENGTH_SHORT).show(); } }
From source file:com.vincestyling.netroid.request.FileDownloadRequest.java
/** * In this method, we got the Content-Length, with the TemporaryFile length, * we can calculate the actually size of the whole file, if TemporaryFile not exists, * we'll take the store file length then compare to actually size, and if equals, * we consider this download was already done. * We used {@link RandomAccessFile} to continue download, when download success, * the TemporaryFile will be rename to StoreFile. *//* w w w . j a v a 2 s . c om*/ @Override public byte[] handleResponse(HttpResponse response, Delivery delivery) throws IOException, ServerError { // Content-Length might be negative when use HttpURLConnection because it default header Accept-Encoding is gzip, // we can force set the Accept-Encoding as identity in prepare() method to slove this problem but also disable gzip response. HttpEntity entity = response.getEntity(); long fileSize = entity.getContentLength(); if (fileSize <= 0) { NetroidLog.d("Response doesn't present Content-Length!"); } long downloadedSize = mTemporaryFile.length(); boolean isSupportRange = HttpUtils.isSupportRange(response); if (isSupportRange) { fileSize += downloadedSize; // Verify the Content-Range Header, to ensure temporary file is part of the whole file. // Sometime, temporary file length add response content-length might greater than actual file length, // in this situation, we consider the temporary file is invalid, then throw an exception. String realRangeValue = HttpUtils.getHeader(response, "Content-Range"); // response Content-Range may be null when "Range=bytes=0-" if (!TextUtils.isEmpty(realRangeValue)) { String assumeRangeValue = "bytes " + downloadedSize + "-" + (fileSize - 1); if (TextUtils.indexOf(realRangeValue, assumeRangeValue) == -1) { throw new IllegalStateException("The Content-Range Header is invalid Assume[" + assumeRangeValue + "] vs Real[" + realRangeValue + "], " + "please remove the temporary file [" + mTemporaryFile + "]."); } } } // Compare the store file size(after download successes have) to server-side Content-Length. // temporary file will rename to store file after download success, so we compare the // Content-Length to ensure this request already download or not. if (fileSize > 0 && mStoreFile.length() == fileSize) { // Rename the store file to temporary file, mock the download success. ^_^ mStoreFile.renameTo(mTemporaryFile); // Deliver download progress. delivery.postDownloadProgress(this, fileSize, fileSize); return null; } RandomAccessFile tmpFileRaf = new RandomAccessFile(mTemporaryFile, "rw"); // If server-side support range download, we seek to last point of the temporary file. if (isSupportRange) { tmpFileRaf.seek(downloadedSize); } else { // If not, truncate the temporary file then start download from beginning. tmpFileRaf.setLength(0); downloadedSize = 0; } InputStream in = null; try { in = entity.getContent(); // Determine the response gzip encoding, support for HttpClientStack download. if (HttpUtils.isGzipContent(response) && !(in instanceof GZIPInputStream)) { in = new GZIPInputStream(in); } byte[] buffer = new byte[6 * 1024]; // 6K buffer int offset; while ((offset = in.read(buffer)) != -1) { tmpFileRaf.write(buffer, 0, offset); downloadedSize += offset; delivery.postDownloadProgress(this, fileSize, downloadedSize); if (isCanceled()) { delivery.postCancel(this); break; } } } finally { try { // Close the InputStream if (in != null) in.close(); } catch (Exception e) { NetroidLog.v("Error occured when calling InputStream.close"); } try { // release the resources by "consuming the content". entity.consumeContent(); } catch (Exception e) { // This can happen if there was an exception above that left the entity in // an invalid state. NetroidLog.v("Error occured when calling consumingContent"); } tmpFileRaf.close(); } return null; }
From source file:net.bluehack.ui.ChatActivity.java
private void searchLinks(final CharSequence charSequence, final boolean force) { if (currentEncryptedChat != null && (MessagesController.getInstance().secretWebpagePreview == 0 || AndroidUtilities.getPeerLayerVersion(currentEncryptedChat.layer) < 46)) { return;/* w ww. j a v a2 s . c o m*/ } if (force && foundWebPage != null) { if (foundWebPage.url != null) { int index = TextUtils.indexOf(charSequence, foundWebPage.url); char lastChar = 0; boolean lenEqual = false; if (index == -1) { if (foundWebPage.display_url != null) { index = TextUtils.indexOf(charSequence, foundWebPage.display_url); lenEqual = index != -1 && index + foundWebPage.display_url.length() == charSequence.length(); lastChar = index != -1 && !lenEqual ? charSequence.charAt(index + foundWebPage.display_url.length()) : 0; } } else { lenEqual = index + foundWebPage.url.length() == charSequence.length(); lastChar = !lenEqual ? charSequence.charAt(index + foundWebPage.url.length()) : 0; } if (index != -1 && (lenEqual || lastChar == ' ' || lastChar == ',' || lastChar == '.' || lastChar == '!' || lastChar == '/')) { return; } } pendingLinkSearchString = null; showReplyPanel(false, null, null, foundWebPage, false, true); } Utilities.searchQueue.postRunnable(new Runnable() { @Override public void run() { if (linkSearchRequestId != 0) { ConnectionsManager.getInstance().cancelRequest(linkSearchRequestId, true); linkSearchRequestId = 0; } ArrayList<CharSequence> urls = null; CharSequence textToCheck; try { Matcher m = AndroidUtilities.WEB_URL.matcher(charSequence); while (m.find()) { if (m.start() > 0) { if (charSequence.charAt(m.start() - 1) == '@') { continue; } } if (urls == null) { urls = new ArrayList<>(); } urls.add(charSequence.subSequence(m.start(), m.end())); } if (urls != null && foundUrls != null && urls.size() == foundUrls.size()) { boolean clear = true; for (int a = 0; a < urls.size(); a++) { if (!TextUtils.equals(urls.get(a), foundUrls.get(a))) { clear = false; } } if (clear) { return; } } foundUrls = urls; if (urls == null) { AndroidUtilities.runOnUIThread(new Runnable() { @Override public void run() { if (foundWebPage != null) { showReplyPanel(false, null, null, foundWebPage, false, true); foundWebPage = null; } } }); return; } textToCheck = TextUtils.join(" ", urls); } catch (Exception e) { FileLog.e("tmessages", e); String text = charSequence.toString().toLowerCase(); if (charSequence.length() < 13 || !text.contains("http://") && !text.contains("https://")) { AndroidUtilities.runOnUIThread(new Runnable() { @Override public void run() { if (foundWebPage != null) { showReplyPanel(false, null, null, foundWebPage, false, true); foundWebPage = null; } } }); return; } textToCheck = charSequence; } if (currentEncryptedChat != null && MessagesController.getInstance().secretWebpagePreview == 2) { AndroidUtilities.runOnUIThread(new Runnable() { @Override public void run() { AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity()); builder.setTitle(LocaleController.getString("AppName", R.string.AppName)); builder.setPositiveButton(LocaleController.getString("OK", R.string.OK), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { MessagesController.getInstance().secretWebpagePreview = 1; ApplicationLoader.applicationContext .getSharedPreferences("mainconfig", Activity.MODE_PRIVATE) .edit() .putInt("secretWebpage2", MessagesController.getInstance().secretWebpagePreview) .commit(); foundUrls = null; searchLinks(charSequence, force); } }); builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null); builder.setMessage(LocaleController.getString("SecretLinkPreviewAlert", R.string.SecretLinkPreviewAlert)); showDialog(builder.create()); MessagesController.getInstance().secretWebpagePreview = 0; ApplicationLoader.applicationContext .getSharedPreferences("mainconfig", Activity.MODE_PRIVATE).edit() .putInt("secretWebpage2", MessagesController.getInstance().secretWebpagePreview) .commit(); } }); return; } final TLRPC.TL_messages_getWebPagePreview req = new TLRPC.TL_messages_getWebPagePreview(); if (textToCheck instanceof String) { req.message = (String) textToCheck; } else { req.message = textToCheck.toString(); } linkSearchRequestId = ConnectionsManager.getInstance().sendRequest(req, new RequestDelegate() { @Override public void run(final TLObject response, final TLRPC.TL_error error) { AndroidUtilities.runOnUIThread(new Runnable() { @Override public void run() { linkSearchRequestId = 0; if (error == null) { if (response instanceof TLRPC.TL_messageMediaWebPage) { foundWebPage = ((TLRPC.TL_messageMediaWebPage) response).webpage; if (foundWebPage instanceof TLRPC.TL_webPage || foundWebPage instanceof TLRPC.TL_webPagePending) { if (foundWebPage instanceof TLRPC.TL_webPagePending) { pendingLinkSearchString = req.message; } if (currentEncryptedChat != null && foundWebPage instanceof TLRPC.TL_webPagePending) { foundWebPage.url = req.message; } showReplyPanel(true, null, null, foundWebPage, false, true); } else { if (foundWebPage != null) { showReplyPanel(false, null, null, foundWebPage, false, true); foundWebPage = null; } } } else { if (foundWebPage != null) { showReplyPanel(false, null, null, foundWebPage, false, true); foundWebPage = null; } } } } }); } }); ConnectionsManager.getInstance().bindRequestToGuid(linkSearchRequestId, classGuid); } }); }
From source file:kr.wdream.ui.ChatActivity.java
private void searchLinks(final CharSequence charSequence, final boolean force) { if (currentEncryptedChat != null && (MessagesController.getInstance().secretWebpagePreview == 0 || AndroidUtilities.getPeerLayerVersion(currentEncryptedChat.layer) < 46)) { return;//from w w w .ja va 2 s.co m } if (force && foundWebPage != null) { if (foundWebPage.url != null) { int index = TextUtils.indexOf(charSequence, foundWebPage.url); char lastChar = 0; boolean lenEqual = false; if (index == -1) { if (foundWebPage.display_url != null) { index = TextUtils.indexOf(charSequence, foundWebPage.display_url); lenEqual = index != -1 && index + foundWebPage.display_url.length() == charSequence.length(); lastChar = index != -1 && !lenEqual ? charSequence.charAt(index + foundWebPage.display_url.length()) : 0; } } else { lenEqual = index + foundWebPage.url.length() == charSequence.length(); lastChar = !lenEqual ? charSequence.charAt(index + foundWebPage.url.length()) : 0; } if (index != -1 && (lenEqual || lastChar == ' ' || lastChar == ',' || lastChar == '.' || lastChar == '!' || lastChar == '/')) { return; } } pendingLinkSearchString = null; showReplyPanel(false, null, null, foundWebPage, false, true); } Utilities.searchQueue.postRunnable(new Runnable() { @Override public void run() { if (linkSearchRequestId != 0) { ConnectionsManager.getInstance().cancelRequest(linkSearchRequestId, true); linkSearchRequestId = 0; } ArrayList<CharSequence> urls = null; CharSequence textToCheck; try { Matcher m = AndroidUtilities.WEB_URL.matcher(charSequence); while (m.find()) { if (m.start() > 0) { if (charSequence.charAt(m.start() - 1) == '@') { continue; } } if (urls == null) { urls = new ArrayList<>(); } urls.add(charSequence.subSequence(m.start(), m.end())); } if (urls != null && foundUrls != null && urls.size() == foundUrls.size()) { boolean clear = true; for (int a = 0; a < urls.size(); a++) { if (!TextUtils.equals(urls.get(a), foundUrls.get(a))) { clear = false; } } if (clear) { return; } } foundUrls = urls; if (urls == null) { AndroidUtilities.runOnUIThread(new Runnable() { @Override public void run() { if (foundWebPage != null) { showReplyPanel(false, null, null, foundWebPage, false, true); foundWebPage = null; } } }); return; } textToCheck = TextUtils.join(" ", urls); } catch (Exception e) { FileLog.e("tmessages", e); String text = charSequence.toString().toLowerCase(); if (charSequence.length() < 13 || !text.contains("http://") && !text.contains("https://")) { AndroidUtilities.runOnUIThread(new Runnable() { @Override public void run() { if (foundWebPage != null) { showReplyPanel(false, null, null, foundWebPage, false, true); foundWebPage = null; } } }); return; } textToCheck = charSequence; } if (currentEncryptedChat != null && MessagesController.getInstance().secretWebpagePreview == 2) { AndroidUtilities.runOnUIThread(new Runnable() { @Override public void run() { AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity()); builder.setTitle( LocaleController.getString("AppName", kr.wdream.storyshop.R.string.AppName)); builder.setPositiveButton( LocaleController.getString("OK", kr.wdream.storyshop.R.string.OK), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { MessagesController.getInstance().secretWebpagePreview = 1; ApplicationLoader.applicationContext .getSharedPreferences("mainconfig", Activity.MODE_PRIVATE) .edit() .putInt("secretWebpage2", MessagesController.getInstance().secretWebpagePreview) .commit(); foundUrls = null; searchLinks(charSequence, force); } }); builder.setNegativeButton( LocaleController.getString("Cancel", kr.wdream.storyshop.R.string.Cancel), null); builder.setMessage(LocaleController.getString("SecretLinkPreviewAlert", kr.wdream.storyshop.R.string.SecretLinkPreviewAlert)); showDialog(builder.create()); MessagesController.getInstance().secretWebpagePreview = 0; ApplicationLoader.applicationContext .getSharedPreferences("mainconfig", Activity.MODE_PRIVATE).edit() .putInt("secretWebpage2", MessagesController.getInstance().secretWebpagePreview) .commit(); } }); return; } final TLRPC.TL_messages_getWebPagePreview req = new TLRPC.TL_messages_getWebPagePreview(); if (textToCheck instanceof String) { req.message = (String) textToCheck; } else { req.message = textToCheck.toString(); } linkSearchRequestId = ConnectionsManager.getInstance().sendRequest(req, new RequestDelegate() { @Override public void run(final TLObject response, final TLRPC.TL_error error) { AndroidUtilities.runOnUIThread(new Runnable() { @Override public void run() { linkSearchRequestId = 0; if (error == null) { if (response instanceof TLRPC.TL_messageMediaWebPage) { foundWebPage = ((TLRPC.TL_messageMediaWebPage) response).webpage; if (foundWebPage instanceof TLRPC.TL_webPage || foundWebPage instanceof TLRPC.TL_webPagePending) { if (foundWebPage instanceof TLRPC.TL_webPagePending) { pendingLinkSearchString = req.message; } if (currentEncryptedChat != null && foundWebPage instanceof TLRPC.TL_webPagePending) { foundWebPage.url = req.message; } showReplyPanel(true, null, null, foundWebPage, false, true); } else { if (foundWebPage != null) { showReplyPanel(false, null, null, foundWebPage, false, true); foundWebPage = null; } } } else { if (foundWebPage != null) { showReplyPanel(false, null, null, foundWebPage, false, true); foundWebPage = null; } } } } }); } }); ConnectionsManager.getInstance().bindRequestToGuid(linkSearchRequestId, classGuid); } }); }