public void handleTextAlign(String textAlign, String verticalAlign) {
    int valign = field ? Gravity.CENTER_VERTICAL : Gravity.TOP;
    int align = Gravity.LEFT;

    if (textAlign != null) {
      if ("left".equals(textAlign)) {
        align = Gravity.LEFT;
      } else if ("center".equals(textAlign)) {
        align = Gravity.CENTER_HORIZONTAL;
      } else if ("right".equals(textAlign)) {
        align = Gravity.RIGHT;
      } else {
        Log.w(LCAT, "Unsupported alignment: " + textAlign);
      }
    } else {
      // Nothing has been set - let's set if something was set previously
      // You can do this with shortcut syntax - but long term maint of code is easier if it's
      // explicit
      Log.w(
          LCAT,
          "No alignment set - old horiz align was: "
              + (tv.getGravity() & Gravity.HORIZONTAL_GRAVITY_MASK));

      if ((tv.getGravity() & Gravity.HORIZONTAL_GRAVITY_MASK) != Gravity.NO_GRAVITY) {
        // Something was set before - so let's use it
        align = tv.getGravity() & Gravity.HORIZONTAL_GRAVITY_MASK;
      }
    }

    if (verticalAlign != null) {
      if ("top".equals(verticalAlign)) {
        valign = Gravity.TOP;
      } else if ("middle".equals(verticalAlign)) {
        valign = Gravity.CENTER_VERTICAL;
      } else if ("bottom".equals(verticalAlign)) {
        valign = Gravity.BOTTOM;
      } else {
        Log.w(LCAT, "Unsupported alignment: " + verticalAlign);
      }
    } else {
      // Nothing has been set - let's set if something was set previously
      // You can do this with shortcut syntax - but long term maint of code is easier if it's
      // explicit
      Log.w(
          LCAT,
          "No alignment set - old vert align was: "
              + (tv.getGravity() & Gravity.VERTICAL_GRAVITY_MASK));

      if ((tv.getGravity() & Gravity.VERTICAL_GRAVITY_MASK) != Gravity.NO_GRAVITY) {
        // Something was set before - so let's use it
        valign = tv.getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
      }
    }

    tv.setGravity(valign | align);
  }
  public void stop() {
    try {
      if (mp != null) {

        if (mp.isPlaying() || isPaused()) {
          if (DBG) {
            Log.d(LCAT, "audio is playing, stop()");
          }
          setState(STATE_STOPPING);
          mp.stop();
          setState(STATE_STOPPED);
          if (remote) {
            stopProgressTimer();
          }
          try {
            mp.prepare();
          } catch (IOException e) {
            Log.e(LCAT, "Error while preparing audio after stop(). Ignoring.");
          } catch (IllegalStateException e) {
            Log.w(LCAT, "Error while preparing audio after stop(). Ignoring.");
          }
        }

        if (isPaused()) {
          paused = false;
        }
      }
    } catch (Throwable t) {
      Log.e(LCAT, "Error : ", t);
    }
  }
  public void play() {
    try {
      if (mp == null) {
        setState(STATE_STARTING);
        initialize();
      }

      if (mp != null) {
        if (!isPlaying()) {
          if (DBG) {
            Log.d(LCAT, "audio is not playing, starting.");
          }
          setVolume(volume);
          if (DBG) {
            Log.d(LCAT, "Play: Volume set to " + volume);
          }
          mp.start();
          setState(STATE_PLAYING);
          paused = false;
          if (remote) {
            startProgressTimer();
          }
        }
        setState(STATE_PLAYING);
      }
    } catch (Throwable t) {
      Log.w(LCAT, "Issue while playing : ", t);
      reset();
    }
  }
 public void removeEventListener(String eventName, int listenerId) {
   if (eventName != null) {
     HashMap<Integer, KrollListener> listeners = eventListeners.get(eventName);
     if (listeners != null) {
       KrollListener listener = listeners.get(listenerId);
       if (listeners.remove(listenerId) == null) {
         if (DBG) {
           Log.w(TAG, "listenerId " + listenerId + " not for eventName '" + eventName + "'");
         }
       } else {
         dispatchOnEventChange(false, eventName, listeners.size(), listener.weakProxy.get());
         if (DBG) {
           Log.i(
               TAG,
               "listener with id "
                   + listenerId
                   + " with eventName '"
                   + eventName
                   + "' was removed.");
         }
       }
     }
   } else {
     throw new IllegalStateException("removeEventListener expects a non-null eventName");
   }
 }
 public void sendAnalytics() {
   if (analyticsIntent != null) {
     if (startService(analyticsIntent) == null) {
       Log.w(LCAT, "Analytics service not found.");
     }
   }
 }
  private void addTabToGroup(TiUITabGroup tg, TabProxy tab) {
    TiTabActivity tta = weakActivity.get();
    if (tta == null) {
      if (DBG) {
        Log.w(LCAT, "Could not add tab because tab activity no longer exists");
      }
    }
    Drawable icon =
        TiDrawableReference.fromObject(getTiContext(), tab.getProperty(TiC.PROPERTY_ICON))
            .getDrawable();
    String tag = TiConvert.toString(tab.getProperty(TiC.PROPERTY_TAG));
    String title = TiConvert.toString(tab.getProperty(TiC.PROPERTY_TITLE));
    if (title == null) {
      title = "";
    }
    tab.setTabGroup(this);
    final WindowProxy vp = (WindowProxy) tab.getProperty(TiC.PROPERTY_WINDOW);
    vp.setTabGroupProxy(this);
    vp.setTabProxy(tab);
    if (tag != null && vp != null) {
      TabSpec tspec = tg.newTab(tag);
      if (icon == null) {
        tspec.setIndicator(title);
      } else {
        tspec.setIndicator(title, icon);
      }

      Intent intent = new Intent(tta, TiActivity.class);
      vp.fillIntentForTab(intent);

      tspec.setContent(intent);

      tg.addTab(tspec);
    }
  }
Example #7
0
 @Override
 public void put(int arg0, Scriptable arg1, Object arg2) {
   // TODO Auto-generated method stub
   super.put(arg0, arg1, arg2);
   if (DBG) {
     Log.w(LCAT, "Put[]");
   }
 }
Example #8
0
 public void hide() {
   if (nativeView != null) {
     nativeView.setVisibility(View.INVISIBLE);
   } else {
     if (DBG) {
       Log.w(LCAT, "Attempt to hide null native control");
     }
   }
 }
Example #9
0
 public void show() {
   if (nativeView != null) {
     nativeView.setVisibility(View.VISIBLE);
   } else {
     if (DBG) {
       Log.w(LCAT, "Attempt to show null native control");
     }
   }
 }
Example #10
0
 private void setAlignment(TextView tv, String textAlign) {
   if ("left".equals(textAlign)) {
     tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
   } else if ("center".equals(textAlign)) {
     tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
   } else if ("right".equals(textAlign)) {
     tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
   } else {
     Log.w(LCAT, "Unsupported alignment: " + textAlign);
   }
 }
Example #11
0
 public void setVolume(float volume) {
   try {
     if (volume < 0.0f) {
       this.volume = 0.0f;
       Log.w(LCAT, "Attempt to set volume less than 0.0. Volume set to 0.0");
     } else if (volume > 1.0) {
       this.volume = 1.0f;
       proxy.setProperty("volume", volume);
       Log.w(LCAT, "Attempt to set volume greater than 1.0. Volume set to 1.0");
     } else {
       this.volume = volume; // Store in 0.0 to 1.0, scale when setting hw
     }
     if (mp != null) {
       float scaledVolume = this.volume;
       mp.setVolume(scaledVolume, scaledVolume);
     }
   } catch (Throwable t) {
     Log.w(LCAT, "Issue while setting volume : ", t);
   }
 }
 public void scheduleRestart(int delay) {
   Log.w(LCAT, "Scheduling application restart");
   if (DBG) {
     Log.d(
         LCAT,
         "Here is call stack leading to restart. (NOTE: this is not a real exception, just a stack trace.) :");
     (new Exception()).printStackTrace();
   }
   if (getRootActivity() != null) {
     getRootActivity().restartActivity(delay);
   }
 }
Example #13
0
 public void setLooping(boolean loop) {
   try {
     if (loop != looping) {
       if (mp != null) {
         mp.setLooping(loop);
       }
       looping = loop;
     }
   } catch (Throwable t) {
     Log.w(LCAT, "Issue while configuring looping : ", t);
   }
 }
 @Kroll.method
 public void insertRowAfter(
     int index, Object data, @Kroll.argument(optional = true) KrollDict options) {
   TiContext ctx = getTiContext();
   if (ctx == null) {
     Log.w(LCAT, "Context has been GC'd, not inserting row.");
     return;
   }
   if (ctx.isUIThread()) {
     handleInsertRowAfter(index, data);
     return;
   }
   sendBlockingUiMessage(MSG_INSERT_ROW, data, INSERT_ROW_AFTER, index);
 }
  @Kroll.method
  public void appendRow(Object row, @Kroll.argument(optional = true) KrollDict options) {
    TiContext ctx = getTiContext();
    if (ctx == null) {
      Log.w(LCAT, "Context has been GC'd, not appending row");
      return;
    }
    if (ctx.isUIThread()) {
      handleAppendRow(row);
      return;
    }

    sendBlockingUiMessage(MSG_APPEND_ROW, row);
  }
  protected TiViewProxy addViewToOldRow(int index, TiUIView titleView, TiViewProxy newViewProxy) {
    if (DBG) {
      Log.w(LCAT, newViewProxy + " was added an old style row, reusing the title TiUILabel");
    }
    LabelProxy label = new LabelProxy(tiContext);
    label.handleCreationDict(titleView.getProxy().getProperties());
    label.setView(titleView);
    label.setModelListener(titleView);
    titleView.setProxy(label);

    getRowProxy().getControls().add(index, label);
    views.add(newViewProxy.getView(tiContext.getActivity()));
    return label;
  }
  public boolean dispatchEvent(String eventName, KrollDict data) {
    boolean dispatched = false;
    if (eventName != null) {
      Map<Integer, KrollListener> listeners = eventListeners.get(eventName);
      if (listeners != null) {
        if (data == null) {
          data = new KrollDict();
        }
        if (!data.containsKey("type")) {
          data.put("type", eventName);
        }

        Set<Entry<Integer, KrollListener>> listenerSet = listeners.entrySet();
        synchronized (eventListeners) {
          for (Entry<Integer, KrollListener> entry : listenerSet) {
            KrollListener listener = entry.getValue();
            if (proxy == null || (proxy != null && listener.isSameProxy(proxy))) {
              boolean invoked = false;
              try {
                if (listener.weakProxy.get() != null) {
                  if (!data.containsKey("source")) {
                    data.put("source", listener.weakProxy.get());
                  }
                  invoked = listener.invoke(eventName, data);
                }
              } catch (Exception e) {
                Log.e(
                    TAG,
                    "Error invoking listener with id "
                        + entry.getKey()
                        + " on eventName '"
                        + eventName
                        + "'",
                    e);
              }
              dispatched = dispatched || invoked;
            }
          }
        }
      } else {
        if (TRACE) {
          Log.w(TAG, "No listeners for eventName: " + eventName);
        }
      }
    } else {
      throw new IllegalStateException("dispatchEvent expects a non-null eventName");
    }
    return dispatched;
  }
Example #18
0
  protected void initialize() throws IOException {
    try {
      mp = new MediaPlayer();
      String url = TiConvert.toString(proxy.getProperty(TiC.PROPERTY_URL));
      if (URLUtil.isAssetUrl(url)) {
        Context context = proxy.getTiContext().getTiApp();
        String path = url.substring(TiConvert.ASSET_URL.length());
        AssetFileDescriptor afd = null;
        try {
          afd = context.getAssets().openFd(path);
          // Why mp.setDataSource(afd) doesn't work is a problem for another day.
          // http://groups.google.com/group/android-developers/browse_thread/thread/225c4c150be92416
          mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
        } catch (IOException e) {
          Log.e(LCAT, "Error setting file descriptor: ", e);
        } finally {
          if (afd != null) {
            afd.close();
          }
        }
      } else {
        Uri uri = Uri.parse(url);
        if (uri.getScheme().equals(TiC.PROPERTY_FILE)) {
          mp.setDataSource(uri.getPath());
        } else {
          remote = true;
          mp.setDataSource(url);
        }
      }

      mp.setLooping(looping);
      mp.setOnCompletionListener(this);
      mp.setOnErrorListener(this);
      mp.setOnInfoListener(this);
      mp.setOnBufferingUpdateListener(this);

      mp.prepare(); // Probably need to allow for Async
      setState(STATE_INITIALIZED);

      setVolume(volume);
      if (proxy.hasProperty(TiC.PROPERTY_TIME)) {
        setTime(TiConvert.toInt(proxy.getProperty(TiC.PROPERTY_TIME)));
      }
    } catch (Throwable t) {
      Log.w(LCAT, "Issue while initializing : ", t);
      release();
      setState(STATE_STOPPED);
    }
  }
 @Kroll.method
 public void deleteRow(int index, @Kroll.argument(optional = true) KrollDict options) {
   TiContext ctx = getTiContext();
   if (ctx == null) {
     Log.w(LCAT, "Context has been GC'd, not deleting row.");
     return;
   }
   if (ctx.isUIThread()) {
     handleDeleteRow(index);
     return;
   }
   Message msg = getUIHandler().obtainMessage(MSG_DELETE_ROW);
   msg.arg1 = index;
   msg.sendToTarget();
 }
Example #20
0
  public void reset() {
    try {
      if (mp != null) {
        if (remote) {
          stopProgressTimer();
        }

        setState(STATE_STOPPING);
        mp.seekTo(0);
        looping = false;
        paused = false;
        setState(STATE_STOPPED);
      }
    } catch (Throwable t) {
      Log.w(LCAT, "Issue while resetting : ", t);
    }
  }
 @Kroll.setProperty
 @Kroll.method
 public void setData(Object[] data, @Kroll.argument(optional = true) KrollDict options) {
   TiContext ctx = getTiContext();
   Object[] actualData = data;
   if (data != null && data.length > 0 && data[0] instanceof Object[]) {
     actualData = (Object[]) data[0];
   }
   if (ctx == null) {
     Log.w(LCAT, "Context has been GC'd, not setting table data.");
     return;
   }
   if (ctx.isUIThread()) {
     handleSetData(actualData);
   } else {
     sendBlockingUiMessage(MSG_SET_DATA, actualData);
   }
 }
Example #22
0
  public String getResponseHeader(String header) {
    String result = "";

    if (readyState > READY_STATE_OPENED) {
      Header h = response.getFirstHeader(header);
      if (h != null) {
        result = h.getValue();
      } else {
        if (DBG) {
          Log.w(LCAT, "No value for respose header: " + header);
        }
      }
    } else {
      throw new IllegalStateException("getResponseHeader can only be called when readyState > 1");
    }

    return result;
  }
    public boolean invoke(String eventName, KrollDict data) {
      boolean invoked = false;
      KrollProxy p = weakProxy.get();
      if (p != null && listener != null) {
        p.fireSingleEvent(eventName, listener, data);
        invoked = true;
      } else {
        if (DBG) {
          Log.w(
              TAG,
              "Unable to fire event with eventName '"
                  + eventName
                  + "' references were garbage collected.");
        }
      }

      return invoked;
    }
Example #24
0
  @Override
  public void processProperties(TiDict d) {
    TiImageView view = getView();

    if (d.containsKey("images")) {
      Object o = d.get("images");
      if (o instanceof Object[]) {
        setImages((Object[]) o);
      }
    } else if (d.containsKey("url")) {
      Log.w(LCAT, "The url property of ImageView is deprecated, use image instead.");
      if (!d.containsKey("image")) {
        d.put("image", d.get("url"));
      }
    }
    if (d.containsKey("canScale")) {
      view.setCanScaleImage(TiConvert.toBoolean(d, "canScale"));
    }
    if (d.containsKey("enableZoomControls")) {
      view.setEnableZoomControls(TiConvert.toBoolean(d, "enableZoomControls"));
    }
    if (d.containsKey("image")) {
      Object image = d.get("image");
      if (image instanceof String) {
        String imageURL = TiConvert.toString(d, "image");
        if (URLUtil.isNetworkUrl(imageURL)) {
          synchronized (imageTokenGenerator) {
            token = imageTokenGenerator.incrementAndGet();
            getView().setImageDrawable(null);
            new BgImageLoader(getProxy().getTiContext(), null, null, token).load(imageURL);
          }
        } else {
          setImage(createBitmap(imageURL));
        }
      } else {
        setImage(createBitmap(image));
      }

    } else {
      getProxy().internalSetDynamicValue("image", null, false);
    }

    super.processProperties(d);
  }
Example #25
0
  protected TiDict eventToTiDict(SensorEvent event, long ts) {
    float x = event.values[0];
    float y = event.values[1];
    float z = event.values[2];

    TiDict heading = new TiDict();
    heading.put("type", EVENT_HEADING);
    heading.put("timestamp", ts);
    heading.put("x", x);
    heading.put("y", y);
    heading.put("z", z);
    heading.put("magneticHeading", x);
    heading.put("accuracy", event.accuracy);
    if (DBG) {
      switch (event.accuracy) {
        case SensorManager.SENSOR_STATUS_UNRELIABLE:
          Log.i(LCAT, "Compass accuracy unreliable");
          break;
        case SensorManager.SENSOR_STATUS_ACCURACY_LOW:
          Log.i(LCAT, "Compass accuracy low");
          break;
        case SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM:
          Log.i(LCAT, "Compass accuracy medium");
          break;
        case SensorManager.SENSOR_STATUS_ACCURACY_HIGH:
          Log.i(LCAT, "Compass accuracy high");
          break;
        default:
          Log.w(LCAT, "Unknown compass accuracy value: " + event.accuracy);
      }
    }
    if (geomagneticField != null) {
      float trueHeading = x - geomagneticField.getDeclination();
      if (trueHeading < 0) {
        trueHeading = 360 - trueHeading;
      }

      heading.put("trueHeading", trueHeading);
    }
    TiDict data = new TiDict();
    data.put("heading", heading);

    return data;
  }
Example #26
0
 public double size() {
   long length = 0;
   InputStream is = null;
   try {
     is = getInputStream();
     length = is.skip(Long.MAX_VALUE);
   } catch (IOException e) {
     Log.w(LCAT, "Error while trying to determine file size: " + e.getMessage());
   } finally {
     if (is != null) {
       try {
         is.close();
       } catch (IOException e) {
         // ignore
       }
     }
   }
   return length;
 }
Example #27
0
 public void pause() {
   try {
     if (mp != null) {
       if (mp.isPlaying()) {
         if (DBG) {
           Log.d(LCAT, "audio is playing, pause");
         }
         if (remote) {
           stopProgressTimer();
         }
         mp.pause();
         paused = true;
         setState(STATE_PAUSED);
       }
     }
   } catch (Throwable t) {
     Log.w(LCAT, "Issue while pausing : ", t);
   }
 }
Example #28
0
  public void release() {
    try {
      if (mp != null) {

        mp.setOnCompletionListener(null);
        mp.setOnErrorListener(null);
        mp.setOnBufferingUpdateListener(null);
        mp.setOnInfoListener(null);

        mp.release();
        mp = null;
        if (DBG) {
          Log.d(LCAT, "Native resources released.");
        }
        remote = false;
      }
    } catch (Throwable t) {
      Log.w(LCAT, "Issue while releasing : ", t);
    }
  }
Example #29
0
 public void run() {
   repeatIndex = 0;
   animating.set(true);
   firedLoad = false;
   topLoop:
   while (isRepeating()) {
     long time = System.currentTimeMillis();
     for (int j = getStart(); isNotFinalFrame(j); j += getCounter()) {
       if (bitmapQueue.size() == 5 && !firedLoad) {
         fireLoad("images");
         firedLoad = true;
       }
       if (paused && !Thread.currentThread().isInterrupted()) {
         try {
           Log.i(LCAT, "Pausing");
           synchronized (loader) {
             loader.wait();
           }
           Log.i(LCAT, "Waking from pause.");
         } catch (InterruptedException e) {
           Log.w(LCAT, "Interrupted from paused state.");
         }
       }
       if (!animating.get()) {
         break topLoop;
       }
       Object image = images[j];
       Bitmap b = createBitmap(image);
       try {
         bitmapQueue.put(new BitmapWithIndex(b, j));
       } catch (InterruptedException e) {
         e.printStackTrace();
       }
       repeatIndex++;
     }
     if (DBG) {
       Log.d(LCAT, "TIME TO LOAD FRAMES: " + (System.currentTimeMillis() - time) + "ms");
     }
   }
   animating.set(false);
 }
  protected void applyChildProxies(TiViewProxy viewProxy, TiUIView view) {
    int i = 0;
    TiViewProxy childProxies[] = viewProxy.getChildren();
    List<TiUIView> childViews = view.getChildren();

    for (TiUIView childView : childViews) {
      if (i == childProxies.length) {
        Log.w(
            LCAT,
            String.format(
                "Number of child proxies (%d) does not match number of child views (%d).",
                childProxies.length, childViews.size()));
        break;
      }

      TiViewProxy childProxy = childProxies[i];
      childView.setProxy(childProxy);
      childView.processProperties(childProxy.getProperties());
      applyChildProxies(childProxy, childView);
      i++;
    }
  }