コード例 #1
0
  @Kroll.method
  public TiBlob getFilteredImage(Object image, @Kroll.argument(optional = true) HashMap options) {
    String cacheKey = null;
    if (image instanceof String) {
      cacheKey = (String) image;
    } else if (image instanceof TiBlob) {
      cacheKey = ((TiBlob) image).getCacheKey();
    } else {
      cacheKey = java.lang.System.identityHashCode(image) + "";
    }
    Drawable drawable = TiUIHelper.buildImageDrawable(getActivity(), image, false, this);
    if (drawable == null) {
      return null;
    }

    Pair<Drawable, KrollDict> result = null;
    if (options != null) {
      if (options.containsKey("callback")) {
        KrollFunction callback = (KrollFunction) options.get("callback");
        options.remove("callback");
        if (callback != null) {
          (new FilterDrawableTask()).execute(this, drawable, options, callback);
          return null;
        }
      }
      result = TiImageHelper.drawableFiltered(drawable, options, cacheKey, false);
    }

    if (result != null) {
      TiBlob blob = TiBlob.blobFromObject(result.first);
      blob.addInfo(result.second);
      return blob;
    }
    return null;
  }
コード例 #2
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;
  }
コード例 #3
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;
 }
コード例 #4
0
  public static KrollDict createDictForImage(TiBlob imageData, String mimeType) {
    KrollDict d = new KrollDict();
    d.putCodeAndMessage(NO_ERROR, null);

    int width = -1;
    int height = -1;

    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inJustDecodeBounds = true;

    // We only need the ContentResolver so it doesn't matter if the root or current activity is used
    // for
    // accessing it
    BitmapFactory.decodeStream(imageData.getInputStream(), null, opts);

    width = opts.outWidth;
    height = opts.outHeight;

    d.put("x", 0);
    d.put("y", 0);
    d.put("width", width);
    d.put("height", height);

    KrollDict cropRect = new KrollDict();
    cropRect.put("x", 0);
    cropRect.put("y", 0);
    cropRect.put("width", width);
    cropRect.put("height", height);
    d.put("cropRect", cropRect);

    d.put("mediaType", MEDIA_TYPE_PHOTO);
    d.put("media", imageData);

    return d;
  }
コード例 #5
0
  public String getResponseText() {
    if (responseData != null && responseText == null) {
      byte[] data = responseData.getBytes();
      if (charset == null) {
        // Detect binary
        int binaryCount = 0;
        int len = data.length;

        if (len > 0) {
          for (int i = 0; i < len; i++) {
            byte b = data[i];
            if (b < 32 || b > 127) {
              if (b != '\n' && b != '\r' && b != '\t' && b != '\b') {
                binaryCount++;
              }
            }
          }

          if ((binaryCount * 100) / len >= IS_BINARY_THRESHOLD) {
            return null;
          }
        }

        charset = HTTP.DEFAULT_CONTENT_CHARSET;
      }

      try {
        responseText = new String(data, charset);
      } catch (UnsupportedEncodingException e) {
        Log.e(LCAT, "Unable to convert to String using charset: " + charset);
      }
    }

    return responseText;
  }
コード例 #6
0
  public TiBlob toBlob() {
    Drawable drawable = getView().getImageDrawable();
    if (drawable != null && drawable instanceof BitmapDrawable) {
      Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
      return TiBlob.blobFromImage(proxy.getTiContext(), bitmap);
    }

    return null;
  }
コード例 #7
0
  public TiBlob toBlob() {
    TiImageView view = getView();
    if (view != null) {
      Drawable drawable = view.getImageDrawable();
      if (drawable != null && drawable instanceof BitmapDrawable) {
        Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
        return bitmap == null ? null : TiBlob.blobFromImage(bitmap);
      }
    }

    return null;
  }
コード例 #8
0
  public void setData(TiBlob blob) {
    reloadMethod = reloadTypes.DATA;
    reloadData = blob;
    String mimeType = "text/html";

    // iOS parity: for whatever reason, in setData, 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);
    }

    if (blob.getType() == TiBlob.TYPE_FILE) {
      String fullPath = blob.getNativePath();
      if (fullPath != null) {
        setUrl(fullPath);
        return;
      }
    }

    if (blob.getMimeType() != null) {
      mimeType = blob.getMimeType();
    }
    if (TiMimeTypeHelper.isBinaryMimeType(mimeType)) {
      getWebView().loadData(blob.toBase64(), mimeType, "base64");
    } else {
      getWebView().loadData(escapeContent(new String(blob.getBytes())), mimeType, "utf-8");
    }
  }
コード例 #9
0
  @Kroll.method
  public TiBlob mask(HashMap args) {
    KrollDict arg = new KrollDict(args);
    TiBlob blob = (TiBlob) arg.get("image");
    TiBlob blob2 = (TiBlob) arg.get("mask");
    TiDrawableReference ref = TiDrawableReference.fromBlob(activity, blob);
    TiDrawableReference ref2 = TiDrawableReference.fromBlob(activity, blob2);

    Bitmap img = mask(ref.getBitmap(), ref2.getBitmap());

    TiBlob result = TiBlob.blobFromImage(img);
    return result;
  }
コード例 #10
0
  KrollDict createDictForImage(int width, int height, byte[] data) {
    KrollDict d = new KrollDict();

    d.put("x", 0);
    d.put("y", 0);
    d.put("width", width);
    d.put("height", height);

    KrollDict cropRect = new KrollDict();
    cropRect.put("x", 0);
    cropRect.put("y", 0);
    cropRect.put("width", width);
    cropRect.put("height", height);
    d.put("cropRect", cropRect);
    d.put("mediaType", MEDIA_TYPE_PHOTO);
    d.put("media", TiBlob.blobFromData(data, "image/png"));

    return d;
  }
コード例 #11
0
  @Override
  public TiBlob read() throws IOException {
    ByteArrayOutputStream baos = null;
    InputStream in = null;
    try {
      baos = new ByteArrayOutputStream();
      in = getInputStream();
      byte buffer[] = new byte[4096];
      while (true) {
        int count = in.read(buffer);
        if (count < 0) {
          break;
        }
        baos.write(buffer, 0, count);
      }
    } finally {
      if (in != null) {
        in.close();
      }
    }

    return TiBlob.blobFromData(
        getTiContext(), baos.toByteArray(), TiMimeTypeHelper.getMimeType(toURL()));
  }
コード例 #12
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));
            }
          }
        });
  }
コード例 #13
0
 public static KrollDict createDictForImage(String path, String mimeType) {
   String[] parts = {path};
   TiBlob imageData =
       TiBlob.blobFromFile(TiFileFactory.createTitaniumFile(parts, false), mimeType);
   return createDictForImage(imageData, mimeType);
 }
コード例 #14
0
 private void setResponseData(HttpEntity entity) throws IOException, ParseException {
   if (entity != null) {
     responseData = TiBlob.blobFromData(proxy.getTiContext(), EntityUtils.toByteArray(entity));
     charset = EntityUtils.getContentCharSet(entity);
   }
 }
コード例 #15
0
    public String handleResponse(HttpResponse response) throws HttpResponseException, IOException {
      connected = true;
      String clientResponse = null;

      if (client != null) {
        TiHTTPClient c = client.get();
        if (c != null) {
          c.response = response;
          c.setReadyState(READY_STATE_HEADERS_RECEIVED);
          c.setStatus(response.getStatusLine().getStatusCode());
          c.setStatusText(response.getStatusLine().getReasonPhrase());
          c.setReadyState(READY_STATE_LOADING);
        }

        if (DBG) {
          try {
            Log.w(LCAT, "Entity Type: " + response.getEntity().getClass());
            Log.w(LCAT, "Entity Content Type: " + response.getEntity().getContentType().getValue());
            Log.w(LCAT, "Entity isChunked: " + response.getEntity().isChunked());
            Log.w(LCAT, "Entity isStreaming: " + response.getEntity().isStreaming());
          } catch (Throwable t) {
            // Ignore
          }
        }

        StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() >= 300) {
          setResponseText(response.getEntity());
          throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
        }

        entity = response.getEntity();
        if (entity.getContentType() != null) {
          contentType = entity.getContentType().getValue();
        }
        KrollCallback onDataStreamCallback = c.getCallback(ON_DATA_STREAM);
        if (onDataStreamCallback != null) {
          is = entity.getContent();
          charset = EntityUtils.getContentCharSet(entity);

          responseData = null;

          if (is != null) {
            final KrollCallback cb = onDataStreamCallback;
            long contentLength = entity.getContentLength();
            if (DBG) {
              Log.d(LCAT, "Content length: " + contentLength);
            }
            int count = 0;
            int totalSize = 0;
            byte[] buf = new byte[4096];
            if (DBG) {
              Log.d(LCAT, "Available: " + is.available());
            }
            if (aborted) {
              if (entity != null) {
                entity.consumeContent();
              }
            } else {
              while ((count = is.read(buf)) != -1) {
                totalSize += count;
                TiDict o = new TiDict();
                o.put("totalCount", contentLength);
                o.put("totalSize", totalSize);
                o.put("size", count);

                byte[] newbuf = new byte[count];
                System.arraycopy(buf, 0, newbuf, 0, count);
                if (responseData == null) {
                  responseData = TiBlob.blobFromData(proxy.getTiContext(), newbuf, contentType);
                } else {
                  responseData.append(TiBlob.blobFromData(proxy.getTiContext(), newbuf));
                }

                TiBlob blob = TiBlob.blobFromData(proxy.getTiContext(), newbuf);
                o.put("blob", blob);
                o.put("progress", ((double) totalSize) / ((double) contentLength));

                cb.callWithProperties(o);
              }
              if (entity != null) {
                try {
                  entity.consumeContent();
                } catch (IOException e) {
                  e.printStackTrace();
                }
              }
            }
          }
        } else {
          setResponseData(entity);
        }
      }
      return clientResponse;
    }
コード例 #16
0
 @Kroll.method
 public void setAttachment(String name, String contentType, TiBlob content) {
   ((UnsavedRevision) revision).setAttachment(name, contentType, content.getInputStream());
 }