I use the following snippet to vibrate the phone in a specific pattern, but it throws and ArrayIndexOutOfBoundsException.
vibrator.vibrate(new long[] { selectedDuration, CONSTANT_DELAY }, REPEAT);
But
vibrator.vibrate(VIBRATE_DURATION);
works fine. Any pointers?
The docs say:
If you want to repeat, pass the index into the pattern at which to start the repeat.
Means REPEAT is only allowed to be 0 or 1 in your case.
This is the implementation:
public void vibrate(long[] pattern, int repeat)
{
// catch this here because the server will do nothing. pattern may
// not be null, let that be checked, because the server will drop it
// anyway
if (repeat < pattern.length) {
try {
mService.vibratePattern(pattern, repeat, mToken);
} catch (RemoteException e) {
}
} else {
throw new ArrayIndexOutOfBoundsException();
}
}
Related
I'm using realm to store a list of Products in my Andorid App.
So, I receive a produtc's list with about 3k objects.
And I'm trying to store them like this:
#Override
public void saveAll(List<ProductsDomain> domainProducts) throws InstantiationException, IllegalAccessException {
Realm instance = getRealmInstance();
RealmList<ProdutcsRealm> realmProducts = new RealmList<ProdutcsRealm>();
try {
ProdutcsRealm realmProduct = getClasseEntidadePersistencia().newInstance();
for (ProductsDomain domainProduct : domainProducts) {
fromDomainToPersistence(domainProduct, realmProduct);
realmProducts.add(realmProduct);
}
instance.beginTransaction();
instance.copyToRealm(realmProducts);// taking to long, 3k items
instance.commitTransaction();
} catch (Exception e) {
e.printStackTrace();
instance.cancelTransaction();
return;
}
}
So, the realm is taking too much time, something like 20 minutes. Anybody have any idea to get better performace?
Solved:
I've found the problem! I was using the same ProductsRealm instance for all iteration. Looks like that Realm dont work well when you try to save a list of multiple references to de same object.
It will be faster if you can add the objects to your Realm in fromDomainToPersistence(). The saveAll() method could be something like:
public void saveAll(...) {
Realm instance = getRealmInstance();
try {
instance.beginTransaction();
for (ProductsDomain domainProduct : domainProducts) {
fromDomainToPersistence(instance, domainProduct);
}
instance.commitTransaction();
} catch (Exception e) {
// ...
}
}
and
public void fromDomainToPersistence(Realm r, DomainProduct domainProduct) {
ProductRealm realmProduct = r.createObject(ProductRealm.class);
// set the fields' values
}
I'm using Ormlite on Android and with the ObjectCache enabled, I get old data back after updating the table with an UpdateBuilder and a ColumnExpression. I have read through the doc and it does not warn against using the UpdateBuilder with the cache enabled.
The settings table should have just 1-5ish rows max. The updateColumnExpression seems like an easy way to allow only one of the rows to be true.
Is this the expected behavior?
public void setActiveSetting(String id)
{
try {
UpdateBuilder<Settings, Integer> updateBuilder2 = getHelper().getSettingsDao().updateBuilder();
updateBuilder2.updateColumnExpression("active", "id = " + id );
updateBuilder2.update();
} catch (SQLException e){
e.printStackTrace();
}
}
And this is the call that returns the outdated data:
public List<Settings> getSettings()
{
List<Settings> settings = null;
try {
settings = getHelper().getSettingsDao().queryForAll();
} catch (SQLException e) {
e.printStackTrace();
}
return settings;
}
And the settings DAO:
public Dao<Settings, Integer> getSettingsDao()
{
if (null == settingsDao) {
try {
settingsDao = getDao(Settings.class);
settingsDao.setObjectCache(true);
} catch (java.sql.SQLException e) {
e.printStackTrace();
}
}
return settingsDao;
}
Disabling the ObjectCache does return the correct data, but this data is fetched quite frequently, so I'd like to keep it.
Thanks
Is this the expected behavior?
Unfortunately, yes. If you had updated the object using dao.update(...); then the cache would know that the object needed to be refreshed. By using the UpdateBuilder to make mass changes to the table, there is no way for the cache to know which objects were affected.
You will need to clear the cache after your call to the UpdateBuilder finishes.
I am doing a program where users can use regular expressions to search in text, and I want to let the matching text to be selected. So I use this code:
public void onClick(View v) {
try {
switch (v.getId()) {
case R.id.btn_search:
Matcher m = Pattern.compile(reg.getText().toString()).matcher(txt.getText());
int start = txt.getSelectionStart();
if (start != txt.getSelectionEnd()) {
start++;
}
if (start < 0 || start >= txt.length()) {
start = 0;
}
while (true) {
try {
m.find(start);
txt.setSelection(m.start(), m.end());
txt.requestFocus();
break;
} catch (IllegalStateException ex) {
if (start == 0) {
err_notfound.show();
break;
}
start = 0;
}
}
break;
}
} catch (PatternSyntaxException ex) {
err_syntax.show();
} catch (Throwable ex) {
showException("onClick", ex);
}
}
However the code is not acting as expected. When I put the cursor manually to a position, and then press the search button, sometimes the program will set the cursor to m.start() but do not expand the selection to m.end(). I have tested the program, and m.start() and m.end() are of different values.
If anyone know what causes the problem, please tell me. I'll appreciate it.
Edit: Thank you for helping! I find an answer to the question. It has something to do with the pin which is used to move the cursor and select text (I don't know what it's called...). If it is shown in the textfield, and setSelection() is called, the EditText will not show the selection correctly. However, if you then use getSelectionStart() and getSelectionEnd(), you'll find they are exactly the same value of m.getStart() and m.getEnd(). This could be a bug. So my solution is to call clearFocus() first. The modified code is like this:
txt.clearFocus();
while (true) {
try {
m.find(start);
txt.setSelection(m.start(), m.end());
txt.requestFocus();
break;
} catch (IllegalStateException ex) {
if (start == 0) {
err_notfound.show();
break;
}
start = 0;
}
}
And it works.
I tested your code and put in one modiication.
Matcher m = Pattern.compile("1*", Pattern.CASE_INSENSITIVE).matcher(txt.getText());
I then made sure that my EditText had only 1's and it highlighted the entire thing.
You many need to confirm that your Regular Expressions are written correctly. You could see more on regualr expressions here(same site I just used).
I've been working with Eclipse ADT for about 2 months. In that time, I have a small utility that allows me to select an IP Address and Port, and then send a file to that combo. The utility works as intended, but when I type in the wrong file name, the application hangs.
#Override
public void run() {
if (data != null) {
this.send(data);
} else if (this.file != null) {
if (file.exists()) {
this.send(file);
} else {
transferError = new FileNotFoundException("The specified file could not be found");
}
}
}
I've even tried to do the following in hopes that one or the other would throw, but I am unsuccessful in both.
public void run() {
if (data != null) {
this.send(data);
} else if (this.file != null) {
if (file.exists()) {
this.send(file);
} else {
transferError = new FileNotFoundException("The specified file could not be found");
}
}try {
throw new Exception("blah blah blah");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I've jockeyed around the exception, I've added the one above, I've tried placing it in different places, and all unsuccessful. Again, I'm exceptionally new to this, and got here from basically mincing various tcp client codes. Aside of creating a way to throw the exception correctly, please help me understand why the first one isn't working and why the one you suggest is.
in your else block you aren't throwin the transferError you create.
throw transferError;
However you probably won't be able to do that because FileNotFoundException is a checked exception and the run() method doesn't declare any thrown exceptions. You probably need to find a different way to present the error to the user, like with a Toast or something.
Your second block doesn't work because you are catching the exception you throw.
I created a small TTS app implementing OnUtteranceCompleteListener and, while things seem to be working exactly as expected, I noticed the following on LogCat (one for each completed utterance):
03-01 20:47:06.436:
VERBOSE/TtsService(381): TTS callback:
dispatch completed to 1
Again, this seems to be benign but I don't understand what '1' means. All such lines for all utterances say "completed to 1", even for utterance IDs that are greater than 1.
What does '1' mean in this log?
BTW, this message is not generated by my code but rather by the TTS engine (Pico) itself.
Looking at the TTSService.java source code available at http://eyes-free.googlecode.com you can find the function dispatchUtteranceCompletedCallback():
private void dispatchUtteranceCompletedCallback(String utteranceId, String packageName) {
/* Legacy support for TTS */
final int oldN = mCallbacksOld.beginBroadcast();
for (int i = 0; i < oldN; i++) {
try {
mCallbacksOld.getBroadcastItem(i).markReached("");
} catch (RemoteException e) {
// The RemoteCallbackList will take care of removing
// the dead object for us.
}
}
try {
mCallbacksOld.finishBroadcast();
} catch (IllegalStateException e) {
// May get an illegal state exception here if there is only
// one app running and it is trying to quit on completion.
// This is the exact scenario triggered by MakeBagel
return;
}
/* End of legacy support for TTS */
ITtsCallbackBeta cb = mCallbacksMap.get(packageName);
if (cb == null) {
return;
}
Log.v(SERVICE_TAG, "TTS callback: dispatch started");
// Broadcast to all clients the new value.
final int N = mCallbacks.beginBroadcast();
try {
cb.utteranceCompleted(utteranceId);
} catch (RemoteException e) {
// The RemoteCallbackList will take care of removing
// the dead object for us.
}
mCallbacks.finishBroadcast();
Log.v(SERVICE_TAG, "TTS callback: dispatch completed to " + N);
}
1 is the current value of N, which is initialized by the return value from mCallbacks.beginBroadcast().
beginBroadcast() is a method of the class RemoteCallbackList and its documentation states that it:
Returns the number of callbacks in the
broadcast, to be used with
getBroadcastItem(int) to determine the
range of indices you can supply
Does this help?