In a legacy code of an Android device I've found following piece of art (definition of interface):
public interface ErrorIdentifiers {
public static final int SUCCESS = 0;
public static final int NOT_AVAILABLE = 1;
public static final int GENERIC_FAILURE = 2;
public static final int REQUEST_NOT_SUPPORTED = 3;
public static final int REQUEST_CANCELLED = 4;
public static final int INVALID_RESPONSE = 5;
public static final int LIST_DOWNLOAD_NOT_FINISHED = 6;
public static final int TIMEOUT_ERROR = 7;
public static final int DIR_FOUND = 0;
public static final int DIR_NOT_FOUND = 1;
public static final int DIR_INVALID_PROVIDER = 2;
public static final int DIR_NO_DATA_CONNECTION = 3;
public static final int DIR_OTHER_ERROR = 4;
// ... and many many more
}
The it's then used like this:
resultcode = ErrorIdentifiers.SUCCESS;
Is it bad to use interface in that way? How to do error identifiers in a proper way?
Nothing wrong when using an interface like this.
However, there's some redundancy in the code. public static final could be removed as redundant, as per JLS 9.3:
Every field declaration in the body of an interface is implicitly public, static, and final. It is permitted to redundantly specify any or all of these modifiers for such fields.
Related
Here, I am attaching the codes of the above mentioned xml files that comes by default after creating a new project. I have tried invalidating cathes & restart and also clean and rebuild but none of them worked for me. I know the problem is similar to the this , but I have not created these files myself, these are being created themselves as I create a new project, earlier these used to be created in correct format but after the updates these are being created in incorrect format.Please someone suggest any solution, is there any bug in this update?
colors.xml
/* AUTO-GENERATED FILE. DO NOT MODIFY.** This class was automatically generated by the* gradle plugin from the resource data it found. It* should not be modified by hand.*/package android.su
ic_launcher_background.xml
/* AUTO-GENERATED FILE. DO NOT MODIFY.** This class was automatically generated by the* gradle plugin from the resource data it found. It* should not be modified by hand.*/package android.support.graphics.drawable;
public final class R {private R() {}
public static final class attr {private attr() {}
public static int font = 0x7f040097;public static int fontProviderAuthority = 0x7f040099;public static int fontProviderCerts = 0x7f04009a;public static int fontProviderFetchStrategy = 0x7f04009b;public static int fontProviderFetchTimeout = 0x7f04009c;public static int fontProviderPackage = 0x7f04009d;public static int fontProviderQuery = 0x7f04009e;public static int fontStyle = 0x7f04009f;public static int fontWeight = 0x7f0400a0;}public static final class bool {private bool() {}
public static int abc_action_bar_embed_tabs = 0x7f050001;}public static final class color {private color() {}
public static int notification_action_color_filter = 0x7f060047;public static int notification_icon_bg_color = 0x7f060048;public static int ripple_material_light = 0x7f060053;public static int secondary_text_default_material_light = 0x7f060055;}public static final class dimen {private dimen() {}
public static int compat_button_inset_horizontal_material = 0x7f08004c;public static int compat_button_inset_vertical_material = 0x7f08004d;public static int compat_button_padding_horizontal_material = 0x7f08004e;public static int compat_button_padding_vertical_material = 0x7f08004f;public static int compat_control_corner_material = 0x7f080050;public static int notification_action_icon_size = 0x7f080086;public static int notification_action_text_size = 0x7f080087;public static int notification_big_circle_margin = 0x7f080088;public static int notification_content_margin_start = 0x7f080089;public static int notification_large_icon_height = 0x7f08008a;public static int notification_large_icon_width = 0x7f08008b;public static int notification_main_column_padding_top = 0x7f08008c;public static int notification_media_narrow_margin = 0x7f08008d;public static int notification_right_icon_size = 0x7f08008e;public static int notification_right_side_padding_top = 0x7f08008f;public static int notification_small_icon_background_padding = 0x7f080090;public static int notification_small_icon_size_as_large = 0x7f080091;public static int notification_subtext_size = 0x7f080092;public static int notification_top_pad = 0x7f080093;public static int notification_top_pad_large_text = 0x7f080094;}public static final class drawable {private drawable() {}
public static int notification_action_background = 0x7f09005d;public static int notification_bg = 0x7f09005e;public static int notification_bg_low = 0x7f09005f;public static int notification_bg_low_normal = 0x7f090060;public static int notification_bg_low_pressed = 0x7f090061;public static int notification_bg_normal = 0x7f090062;public static int notification_bg_normal_pressed = 0x7f090063;public static int notification_icon_background = 0x7f090064;public static int notification_template_icon_bg = 0x7f090065;public static int notification_template_icon_low_bg = 0x7f090066;public static int notification_tile_bg = 0x7f090067;public static int notify_panel_notification_icon_bg = 0x7f090068;}public static final class id {private id() {}
public static int action_container = 0x7f0c0009;public static int action_divider = 0x7f0c000b;public static int action_image = 0x7f0c000c;public static int action_text = 0x7f0c0012;public static int actions = 0x7f0c0013;public static int async = 0x7f0c0017;public static int blocking = 0x7f0c0019;public static int chronometer = 0x7f0c001f;public static int forever = 0x7f0c0033;public static int icon = 0x7f0c0036;public static int icon_group = 0x7f0c0037;public static int info = 0x7f0c0039;public static int italic = 0x7f0c003a;public static int line1 = 0x7f0c003e;public static int line3 = 0x7f0c003f;public static int normal = 0x7f0c0049;public static int notification_background = 0x7f0c004a;public static int notification_main_column = 0x7f0c004b;public static i
ic_launcher_foreground.xml
/* AUTO-GENERATED FILE. DO NOT MODIFY.** This class was automatically generated by the* gradle plugin from the resource data it found. It* should not be modified by hand.*/package android.support.graphics.drawable.animated;
public final class R {private R() {}
public static final class attr {private attr() {}
public static int coordinatorLayoutStyle = 0x7f04006d;public static int font = 0x7f040097;public static int fontProviderAuthority = 0x7f040099;public static int fontProviderCerts = 0x7f04009a;public static int fontProviderFetchStrategy = 0x7f04009b;public static int fontProviderFetchTimeout = 0x7f04009c;public static int fontProviderPackage = 0x7f04009d;public static int fontProviderQuery = 0x7f04009e;public static int fontStyle = 0x7f04009f;public static int fontWeight = 0x7f0400a0;public static int keylines = 0x7f0400ba;public static int layout_anchor = 0x7f0400bf;public static int layout_anchorGravity = 0x7f0400c0;public static int layout_behavior = 0x7f0400c1;public static int layout_dodgeInsetEdges = 0x7f0400c4;public static int layout_insetEdge = 0x7f0400c5;public static int layout_keyline = 0x7f0400c6;public static int statusBarBackground = 0x7f040116;}public static final class bool {private bool() {}
public static int abc_action_bar_embed_tabs = 0x7f050001;}public static final class color {private color() {}
public static int notification_action_color_filter = 0x7f060047;public static int notification_icon_bg_color = 0x7f060048;public static int ripple_material_light = 0x7f060053;public static int secondary_text_default_
Those aren't XML files, and the error therefore indicates an immediate catastrophic parsing failure.
database contacts2.db from android has a table called data which has column called mimetype_id which correspond to the string representation found in another table called mimetypes. However what I do not understand is: lets say we have an example with the Phone class directly taken from the package android.provider;
public static final class Phone implements ContactsContract.DataColumnsWithJoins, ContactsContract.CommonDataKinds.CommonColumns {
public static final Uri CONTENT_FILTER_URI = null;
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/phone_v2";
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/phone_v2";
public static final Uri CONTENT_URI = null;
public static final Uri ENTERPRISE_CONTENT_FILTER_URI = null;
public static final String EXTRA_ADDRESS_BOOK_INDEX = "android.provider.extra.ADDRESS_BOOK_INDEX";
public static final String EXTRA_ADDRESS_BOOK_INDEX_COUNTS = "android.provider.extra.ADDRESS_BOOK_INDEX_COUNTS";
public static final String EXTRA_ADDRESS_BOOK_INDEX_TITLES = "android.provider.extra.ADDRESS_BOOK_INDEX_TITLES";
public static final String NORMALIZED_NUMBER = "data4";
public static final String NUMBER = "data1";
public static final String SEARCH_DISPLAY_NAME_KEY = "search_display_name";
public static final String SEARCH_PHONE_NUMBER_KEY = "search_phone_number";
public static final int TYPE_ASSISTANT = 19;
public static final int TYPE_CALLBACK = 8;
public static final int TYPE_CAR = 9;
public static final int TYPE_COMPANY_MAIN = 10;
public static final int TYPE_FAX_HOME = 5;
public static final int TYPE_FAX_WORK = 4;
public static final int TYPE_HOME = 1;
public static final int TYPE_ISDN = 11;
public static final int TYPE_MAIN = 12;
public static final int TYPE_MMS = 20;
public static final int TYPE_MOBILE = 2;
public static final int TYPE_OTHER = 7;
public static final int TYPE_OTHER_FAX = 13;
public static final int TYPE_PAGER = 6;
public static final int TYPE_RADIO = 14;
public static final int TYPE_TELEX = 15;
public static final int TYPE_TTY_TDD = 16;
public static final int TYPE_WORK = 3;
public static final int TYPE_WORK_MOBILE = 17;
public static final int TYPE_WORK_PAGER = 18;
Now there we see different types for example “TYPE_ISDN = 11” or TYPE_MOBILE = 2; I do understand that we have different data colums in the data table i.e. “data1” or “data4” in this case but where is the ISDN type information stored in the database.
i.e. the question is : how can I tell directly from reading the data table if the phone I just read it is a mobile phone and not fax and vise versa . Where would be those integers 11 or 2 saved in this case? Since I have 5 phones for example and they are tagged with home, work, etc. etc.
I am creating an app in which i want that when i click in scan button it will only scan for BLEs not android phone's bluetooth, so is there any permission or dependency to scan only for BLE's not centrals.
You can try this for avoiding Phone:
if (device.getBluetoothClass().getMajorDeviceClass() != BluetoothClass.Device.Major.PHONE) {
// do something
}
Pls see BluetoothClass.Device.Major and choose what u want
public static class Major {
private static final int BITMASK = 0x1F00;
public static final int MISC = 0x0000;
public static final int COMPUTER = 0x0100;
public static final int PHONE = 0x0200;
public static final int NETWORKING = 0x0300;
public static final int AUDIO_VIDEO = 0x0400;
public static final int PERIPHERAL = 0x0500;
public static final int IMAGING = 0x0600;
public static final int WEARABLE = 0x0700;
public static final int TOY = 0x0800;
public static final int HEALTH = 0x0900;
public static final int UNCATEGORIZED = 0x1F00;
}
Problem
Why some constants are under the public modifier while some other private? Are those under public can be called from applications that use the library? If so, how to call the constant from an app, is it like this: CertainLibraryClass.ActivityResultCode.CODE_A?
Code
public class CertainLibraryClass {
public class ActivityResultCode {
public static final int CODE_A = 0X02;
public static final int CODE_B = 0X03;
public static final int CODE_C = 0X04;
}
public class VersionCode {
private static final int VERSION_MAJOR = 1;
private static final int VERSION_MINOR1 = 0;
private static final int VERSION_MINOR2 = 2;
}
// ....
}
Why some constants are under the public modifier?
Ans: So that all other classes can access it e.g.RESULT_OK,SUCCESS.
Why some constants are under the private modifier?
Ans:So that only that class can access it
e.g. consider you are calling getId() libarary function from your class
public class CertainLibraryClass {
private static int ID=0;
public static int getId()
{
return ID+1;
}
here you are not accessing ID field directly ,instead you are calling getId() function which ultimately returns the id, it means that ID variable is internally used by CertainLibraryClass class
As title says, I suspect my R.string.app_name changed. Since my notification sometimes never update any more, but code below did executed (I can see it in logcat). Is that possible?
nm.notify(R.string.app_name, notification);
Log.i(TAG, "B:showNotification(): id = " + R.string.app_name);
No, this is not possible as all R.*.* resources are defined as constants in your R.java file (in the /gen/<your-package> folder). These are not edited by Android later on.
Example:
public final class R {
public static final class anim {
public static final int accelerate_interpolator=0x7f040000;
public static final int activity_slide_in=0x7f040001;
public static final int activity_slide_out=0x7f040002;
}
public static final class attr {
}
public static final class drawable {
public static final int ic_action_search=0x7f020000;
public static final int ic_launcher=0x7f020001;
}
public static final class id {
public static final int main_layout=0x7f070001;
public static final int scroll=0x7f070000;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f050000;
}
public static final class style {
public static final int AppTheme=0x7f060000;
}
}
This is the R.java file from one of my real projects. As you can see, each value is a final int which is assigned when you're developing the app (all values are assigned using hex, so you'd have to use an online converter to the actual int value)