Пример #1
0
 public Bitmap createBitmap(Object image) {
   if (image instanceof TiBlob) {
     TiBlob blob = (TiBlob) image;
     return TiUIHelper.createBitmap(blob.getInputStream());
   } else if (image instanceof FileProxy) {
     FileProxy file = (FileProxy) image;
     try {
       return TiUIHelper.createBitmap(file.getBaseFile().getInputStream());
     } catch (IOException e) {
       Log.e(
           LCAT,
           "Error creating drawable from file: " + file.getBaseFile().getNativeFile().getName(),
           e);
     }
   } else if (image instanceof String) {
     String url = proxy.getTiContext().resolveUrl(null, (String) image);
     TiBaseFile file =
         TiFileFactory.createTitaniumFile(proxy.getTiContext(), new String[] {url}, false);
     try {
       return TiUIHelper.createBitmap(file.getInputStream());
     } catch (IOException e) {
       Log.e(LCAT, "Error creating drawable from path: " + image.toString(), e);
     }
   } else if (image instanceof TiDict) {
     TiBlob blob = TiUIHelper.getImageFromDict((TiDict) image);
     if (blob != null) {
       return TiUIHelper.createBitmap(blob.getInputStream());
     } else {
       Log.e(LCAT, "Couldn't find valid image in object: " + image.toString());
     }
   }
   return null;
 }
 public void setBackgroundImageProperty(TiDict d, String property) {
   String path = TiConvert.toString(d, property);
   String url = tiContext.resolveUrl(null, path);
   TiBaseFile file = TiFileFactory.createTitaniumFile(tiContext, new String[] {url}, false);
   try {
     setBackgroundDrawable(new BitmapDrawable(TiUIHelper.createBitmap(file.getInputStream())));
   } catch (IOException e) {
     Log.e(LCAT, "Error creating background image from path: " + path.toString(), e);
   }
 }
Пример #3
0
  public void setUrl(String url) {
    if (url == null) return;
    reloadMethod = reloadTypes.URL;
    reloadData = url;
    String finalUrl = url;
    Uri uri = Uri.parse(finalUrl);
    boolean originalUrlHasScheme = (uri.getScheme() != null);

    if (!originalUrlHasScheme) {
      finalUrl = getProxy().resolveUrl(null, finalUrl);
    }

    if (TiFileFactory.isLocalScheme(finalUrl) && mightBeHtml(finalUrl)) {
      TiBaseFile tiFile = TiFileFactory.createTitaniumFile(finalUrl, false);
      if (tiFile != null) {
        StringBuilder out = new StringBuilder();
        InputStream fis = null;
        try {
          fis = tiFile.getInputStream();
          InputStreamReader reader = new InputStreamReader(fis, "utf-8");
          BufferedReader breader = new BufferedReader(reader);
          String line = breader.readLine();
          while (line != null) {
            if (!bindingCodeInjected) {
              int pos = line.indexOf("<html");
              if (pos >= 0) {
                int posEnd = line.indexOf(">", pos);
                if (posEnd > pos) {
                  out.append(line.substring(pos, posEnd + 1));
                  out.append(TiWebViewBinding.SCRIPT_TAG_INJECTION_CODE);
                  if ((posEnd + 1) < line.length()) {
                    out.append(line.substring(posEnd + 1));
                  }
                  out.append("\n");
                  bindingCodeInjected = true;
                  line = breader.readLine();
                  continue;
                }
              }
            }
            out.append(line);
            out.append("\n");
            line = breader.readLine();
          }
          setHtmlInternal(
              out.toString(),
              (originalUrlHasScheme ? url : finalUrl),
              "text/html"); // keep app:// etc. intact in case
          // html in file contains links
          // to JS that use app:// etc.
          return;
        } catch (IOException ioe) {
          Log.e(
              TAG,
              "Problem reading from "
                  + url
                  + ": "
                  + ioe.getMessage()
                  + ". Will let WebView try loading it directly.",
              ioe);
        } finally {
          if (fis != null) {
            try {
              fis.close();
            } catch (IOException e) {
              Log.w(TAG, "Problem closing stream: " + e.getMessage(), e);
            }
          }
        }
      }
    }

    Log.d(TAG, "WebView will load " + url + " directly without code injection.", Log.DEBUG_MODE);
    // iOS parity: for whatever reason, when a remote url is used, the iOS implementation
    // explicitly sets the native webview's setScalesPageToFit to YES if the
    // Ti scalesPageToFit property has _not_ been set.
    if (!proxy.hasProperty(TiC.PROPERTY_SCALES_PAGE_TO_FIT)) {
      getWebView().getSettings().setLoadWithOverviewMode(true);
    }
    isLocalHTML = false;
    getWebView().loadUrl(finalUrl);
  }