Make good use of TextUtils.isEmpty()

Keep a consistent style when checking for empty strings. This also adds some
missing lentgh() > 0 checks in crucial places like AppDetails.
This commit is contained in:
Daniel Martí 2015-06-28 12:42:30 -07:00
parent 7c49f03f21
commit 2b15a3c17b
8 changed files with 31 additions and 25 deletions

View File

@ -48,6 +48,7 @@ import android.text.Layout;
import android.text.Selection;
import android.text.Spannable;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
@ -190,12 +191,12 @@ public class AppDetails extends ActionBarActivity implements ProgressListener, A
Log.w(TAG, "Application " + app.id + " is not installed anymore");
return getString(R.string.not_inst);
}
if (installerPkgName != null && installerPkgName.length() > 0) {
final String installerLabel = InstalledAppProvider
.getApplicationLabel(mctx, installerPkgName);
return getString(R.string.inst_known_source, installerLabel);
if (TextUtils.isEmpty(installerPkgName)) {
return getString(R.string.inst_unknown_source);
}
return getString(R.string.inst_unknown_source);
final String installerLabel = InstalledAppProvider
.getApplicationLabel(mctx, installerPkgName);
return getString(R.string.inst_known_source, installerLabel);
}
@Override
@ -567,7 +568,7 @@ public class AppDetails extends ActionBarActivity implements ProgressListener, A
Log.d(TAG, "Getting application details for " + appId);
App newApp = null;
if (appId != null && appId.length() > 0) {
if (!TextUtils.isEmpty(appId)) {
newApp = AppProvider.Helper.findById(getContentResolver(), appId);
}
@ -1180,63 +1181,63 @@ public class AppDetails extends ActionBarActivity implements ProgressListener, A
// Website button
TextView tv = (TextView) view.findViewById(R.id.website);
if (getApp().webURL != null)
if (!TextUtils.isEmpty(getApp().webURL))
tv.setOnClickListener(mOnClickListener);
else
tv.setVisibility(View.GONE);
// Source button
tv = (TextView) view.findViewById(R.id.source);
if (getApp().sourceURL != null)
if (!TextUtils.isEmpty(getApp().sourceURL))
tv.setOnClickListener(mOnClickListener);
else
tv.setVisibility(View.GONE);
// Issues button
tv = (TextView) view.findViewById(R.id.issues);
if (getApp().trackerURL != null)
if (!TextUtils.isEmpty(getApp().trackerURL))
tv.setOnClickListener(mOnClickListener);
else
tv.setVisibility(View.GONE);
// Changelog button
tv = (TextView) view.findViewById(R.id.changelog);
if (getApp().changelogURL != null)
if (!TextUtils.isEmpty(getApp().changelogURL))
tv.setOnClickListener(mOnClickListener);
else
tv.setVisibility(View.GONE);
// Donate button
tv = (TextView) view.findViewById(R.id.donate);
if (getApp().donateURL != null)
if (!TextUtils.isEmpty(getApp().donateURL))
tv.setOnClickListener(mOnClickListener);
else
tv.setVisibility(View.GONE);
// Bitcoin
tv = (TextView) view.findViewById(R.id.bitcoin);
if (getApp().bitcoinAddr != null)
if (!TextUtils.isEmpty(getApp().bitcoinAddr))
tv.setOnClickListener(mOnClickListener);
else
tv.setVisibility(View.GONE);
// Litecoin
tv = (TextView) view.findViewById(R.id.litecoin);
if (getApp().litecoinAddr != null)
if (!TextUtils.isEmpty(getApp().litecoinAddr))
tv.setOnClickListener(mOnClickListener);
else
tv.setVisibility(View.GONE);
// Dogecoin
tv = (TextView) view.findViewById(R.id.dogecoin);
if (getApp().dogecoinAddr != null)
if (!TextUtils.isEmpty(getApp().dogecoinAddr))
tv.setOnClickListener(mOnClickListener);
else
tv.setVisibility(View.GONE);
// Flattr
tv = (TextView) view.findViewById(R.id.flattr);
if (getApp().flattrID != null)
if (!TextUtils.isEmpty(getApp().flattrID))
tv.setOnClickListener(mOnClickListener);
else
tv.setVisibility(View.GONE);

View File

@ -187,11 +187,11 @@ public class FDroid extends ActionBarActivity {
}
Intent call = null;
if (appId != null && appId.length() > 0) {
if (!TextUtils.isEmpty(appId)) {
Log.d(TAG, "FDroid launched via app link for '" + appId + "'");
call = new Intent(this, AppDetails.class);
call.putExtra(AppDetails.EXTRA_APPID, appId);
} else if (query != null && query.length() > 0) {
} else if (!TextUtils.isEmpty(query)) {
Log.d(TAG, "FDroid launched via search link for '" + query + "'");
call = new Intent(this, SearchResults.class);
call.setAction(Intent.ACTION_SEARCH);

View File

@ -20,6 +20,7 @@
package org.fdroid.fdroid;
import android.os.Bundle;
import android.text.TextUtils;
import org.fdroid.fdroid.data.Apk;
import org.fdroid.fdroid.data.App;
@ -97,7 +98,7 @@ public class RepoXMLHandler extends DefaultHandler {
super.endElement(uri, localName, qName);
final String curel = localName;
final String str = curchars.toString().trim();
final boolean empty = (str == null || str.length() == 0);
final boolean empty = TextUtils.isEmpty(str);
if (curel.equals("application") && curapp != null) {
apps.add(curapp);

View File

@ -266,8 +266,9 @@ public class UpdateService extends IntentService implements ProgressListener {
protected void sendStatus(int statusCode, String message) {
if (receiver != null) {
Bundle resultData = new Bundle();
if (message != null && message.length() > 0)
if (!TextUtils.isEmpty(message)) {
resultData.putString(RESULT_MESSAGE, message);
}
receiver.send(statusCode, resultData);
}
}

View File

@ -465,7 +465,7 @@ public final class Utils {
}
public static CommaSeparatedList make(String list) {
if (list == null || list.length() == 0)
if (TextUtils.isEmpty(list))
return null;
return new CommaSeparatedList(list);
}

View File

@ -1,5 +1,7 @@
package org.fdroid.fdroid.data;
import android.text.TextUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -36,7 +38,7 @@ public class QuerySelection {
public String getSelection() { return selection; }
public boolean hasSelection() {
return selection != null && selection.length() > 0;
return !TextUtils.isEmpty(selection);
}
public boolean hasArgs() {

View File

@ -476,7 +476,7 @@ public class ManageReposActivity extends ActionBarActivity {
// to the user until they try to save the repo.
}
final Repo repo = uri.length() > 0 ? RepoProvider.Helper.findByAddress(context, uri) : null;
final Repo repo = !TextUtils.isEmpty(uri) ? RepoProvider.Helper.findByAddress(context, uri) : null;
if (repo == null) {
repoDoesntExist();
@ -675,7 +675,7 @@ public class ManageReposActivity extends ActionBarActivity {
}
ContentValues values = new ContentValues(2);
values.put(RepoProvider.DataColumns.ADDRESS, address);
if (fingerprint != null && fingerprint.length() > 0) {
if (!TextUtils.isEmpty(fingerprint)) {
values.put(RepoProvider.DataColumns.FINGERPRINT, fingerprint.toUpperCase(Locale.ENGLISH));
}
RepoProvider.Helper.insert(context, values);
@ -689,7 +689,7 @@ public class ManageReposActivity extends ActionBarActivity {
private void updateAndEnableExistingRepo(String url, String fingerprint) {
if (fingerprint != null) {
fingerprint = fingerprint.trim();
if (fingerprint.length() == 0) {
if (TextUtils.isEmpty(fingerprint)) {
fingerprint = null;
} else {
fingerprint = fingerprint.toUpperCase(Locale.ENGLISH);

View File

@ -9,6 +9,7 @@ import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
@ -87,7 +88,7 @@ public class SearchResultsFragment extends ListFragment implements LoaderManager
if (query != null)
query = query.trim();
if (query == null || query.length() == 0)
if (TextUtils.isEmpty(query))
getActivity().finish();
TextView tv = (TextView) view.findViewById(R.id.description);