Ejemplo n.º 1
0
  public int addTitaniumFileAsPostData(String name, Object value) {
    try {
      if (value instanceof TiBaseFile) {
        TiBaseFile baseFile = (TiBaseFile) value;
        FileBody body =
            new FileBody(
                baseFile.getNativeFile(), TiMimeTypeHelper.getMimeType(baseFile.nativePath()));
        parts.put(name, body);
        return (int) baseFile.getNativeFile().length();
      } else if (value instanceof TiBlob) {
        TiBlob blob = (TiBlob) value;
        String mimeType = blob.getMimeType();
        File tmpFile =
            File.createTempFile(
                "tixhr", TiMimeTypeHelper.getFileExtensionFromMimeType(mimeType, ".txt"));
        FileOutputStream fos = new FileOutputStream(tmpFile);
        fos.write(blob.getBytes());
        fos.close();

        FileBody body = new FileBody(tmpFile, mimeType);
        parts.put(name, body);
        return blob.getLength();
      } else {
        if (value != null) {
          Log.e(LCAT, name + " is a " + value.getClass().getSimpleName());
        } else {
          Log.e(LCAT, name + " is null");
        }
      }
    } catch (IOException e) {
      Log.e(LCAT, "Error adding post data (" + name + "): " + e.getMessage());
    }
    return 0;
  }
Ejemplo n.º 2
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);
   }
 }
Ejemplo n.º 4
0
  @Kroll.method
  public void previewImage(KrollDict options) {
    Activity activity = TiApplication.getAppCurrentActivity();
    if (activity == null) {
      Log.w(TAG, "Unable to get current activity for previewImage.", Log.DEBUG_MODE);
      return;
    }

    KrollFunction successCallback = null;
    KrollFunction errorCallback = null;
    TiBlob image = null;

    if (options.containsKey("success")) {
      successCallback = (KrollFunction) options.get("success");
    }
    if (options.containsKey("error")) {
      errorCallback = (KrollFunction) options.get("error");
    }
    if (options.containsKey("image")) {
      image = (TiBlob) options.get("image");
    }

    if (image == null) {
      if (errorCallback != null) {
        errorCallback.callAsync(
            getKrollObject(), createErrorResponse(UNKNOWN_ERROR, "Missing image property"));
      }
    }

    TiBaseFile f = (TiBaseFile) image.getData();

    final KrollFunction fSuccessCallback = successCallback;
    final KrollFunction fErrorCallback = errorCallback;

    Log.d(TAG, "openPhotoGallery called", Log.DEBUG_MODE);

    TiActivitySupport activitySupport = (TiActivitySupport) activity;

    Intent intent = new Intent(Intent.ACTION_VIEW);
    TiIntentWrapper previewIntent = new TiIntentWrapper(intent);
    String mimeType = image.getMimeType();

    if (mimeType != null && mimeType.length() > 0) {
      intent.setDataAndType(Uri.parse(f.nativePath()), mimeType);
    } else {
      intent.setData(Uri.parse(f.nativePath()));
    }

    previewIntent.setWindowId(TiIntentWrapper.createActivityName("PREVIEW"));

    final int code = activitySupport.getUniqueResultCode();
    activitySupport.launchActivityForResult(
        intent,
        code,
        new TiActivityResultHandler() {

          public void onResult(Activity activity, int requestCode, int resultCode, Intent data) {
            Log.e(TAG, "OnResult called: " + resultCode);
            if (fSuccessCallback != null) {
              KrollDict response = new KrollDict();
              response.putCodeAndMessage(NO_ERROR, null);
              fSuccessCallback.callAsync(getKrollObject(), response);
            }
          }

          public void onError(Activity activity, int requestCode, Exception e) {
            String msg = "Gallery problem: " + e.getMessage();
            Log.e(TAG, msg, e);
            if (fErrorCallback != null) {
              fErrorCallback.callAsync(getKrollObject(), createErrorResponse(UNKNOWN_ERROR, msg));
            }
          }
        });
  }
Ejemplo n.º 5
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);
  }