0
\$\begingroup\$

What's a more concise way to do this? It's way too verbose for my taste.

    @Override
    protected void onResume() {
    super.onResume();
    Thread.currentThread().setPriority(Thread.MAX_PRIORITY - 1);
    TinyDB tinydb = new TinyDB(this);
    Log.v(TAG + "BaseStartup.onResume", "onResume");
    IntentFilter intentFilter = new IntentFilter(); 
    intentFilter.addAction("tv.apps.v4.ve");
    intentFilter.addAction("tv.apps.v4.ev");
    intentFilter.addAction("tv.apps.v4.we");
    intentFilter.addAction("tv.apps.v4.wef");
    intentFilter.addAction("tv.apps.v4.wefew");
    intentFilter.addAction("tv.apps.v4.gwe");
    intentFilter.addAction("tv.apps.v4.ewew");
    intentFilter.addAction("SMS_ewe");
    intentFilter.addAction("INTERNET_BACK_UPweeew");
    intentFilter.addAction("tv.apps.v4.weewwee");
    intentFilter.addAction("tv.apps.weewe");
    intentFilter.addAction("tv.apps.weet");
    intentFilter.addAction("tv.apps.weewewewees");
   // Get source of open for app re-engagement
    mobileAppTracker.setReferralSources(this);
    // MAT will not function unless the measureSession call is included
    mobileAppTracker.measureSession();
    //sceneTransport("resumeSession", "null", "null" );

}
\$\endgroup\$
  • \$\begingroup\$ What does Log.v(...) stand for? View, verify, verbose, ...? I'd rather use a more descriptive name. \$\endgroup\$ – GeroldBroser reinstates Monica Nov 26 '14 at 4:15
1
\$\begingroup\$

You could use a loop to reduce the duplication:

List<String> extensions = Arrays.asList(
    "ve",
    "ev",
    "we",
    "wef"
    // ...
);
for (String ext : extensions) {
    intentFilter.addAction("tv.apps.v4." + ext);
}

But actually, it would be better to move hardcoded strings outside of java code. You could create a res/values/arrays.xml file like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="actions">
        <item>tv.apps.v4.ve</item>
        <item>tv.apps.v4.ev</item>
        <item>tv.apps.v4.we</item>
        <!--
        <item>...</item>
        -->
    </string-array>
</resources>

And use it in the code like this:

for (String action : getResources().getStringArray(R.array.actions)) {
    intentFilter.addAction(action);
}

Does this work as intended?

Thread.currentThread().setPriority(Thread.MAX_PRIORITY - 1);

I don't know about this particular usage, but I do know that low level thread operations are not recommended in Android development in general.

Unless this usage is explicitly mentioned somewhere in the official guides, I'm not sure if this will work reliably at all.

\$\endgroup\$
0
\$\begingroup\$

One or even more additional addActions(...) methods in IntentFilter are another option:

public void addActions(String... actions) { ... }
public void addActions(String[] actions) { ... }
public void addActions(Collection<String> actions) { ... }

Following janos' answer and refactoring the loop into IntentFilter there's one line remaining in onResume() by using the second of those above:

addActions(getResources().getStringArray(R.array.actions));

Furthermore, I guess there is already a String[] actions; or Collection<String> actions; inside IntentFilter to hold the actions. (What else can it be?)

Then you can remove the loop completely and use:

addActions(getResources().getStringArray(R.array.actions));

or

addActions(new HashSet<String>(Arrays.asList(getResources().getStringArray(R.array.actions))));
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.