{"record":{"id":"dfd9d49cb35c32a3","repo":"microg/GmsCore","slug":"granularity-granularity-must-be-a-granularity-gr","errorCode":null,"errorMessage":"granularity {granularity} must be a Granularity.GRANULARITY_* constant","messagePattern":"granularity (.+?) must be a Granularity\\.GRANULARITY_\\* constant","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"play-services-location/src/main/java/org/microg/gms/location/GranularityUtil.java","lineNumber":25,"sourceCode":"\nimport com.google.android.gms.location.Granularity;\nimport com.google.android.gms.location.Priority;\n\npublic class GranularityUtil {\n    public static boolean isValidGranularity(int granularity) {\n        switch (granularity) {\n            default:\n                return false;\n            case Granularity.GRANULARITY_PERMISSION_LEVEL:\n            case Granularity.GRANULARITY_COARSE:\n            case Granularity.GRANULARITY_FINE:\n                return true;\n        }\n    }\n\n    public static int checkValidGranularity(int granularity) {\n        if (!isValidGranularity(granularity)) {\n            throw new IllegalArgumentException(\"granularity \" + granularity + \" must be a Granularity.GRANULARITY_* constant\");\n        }\n        return granularity;\n    }\n\n    public static String granularityToString(int granularity) {\n        switch (granularity) {\n            case Granularity.GRANULARITY_PERMISSION_LEVEL:\n                return \"GRANULARITY_PERMISSION_LEVEL\";\n            case Granularity.GRANULARITY_COARSE:\n                return \"GRANULARITY_COARSE\";\n            case Granularity.GRANULARITY_FINE:\n                return \"GRANULARITY_FINE\";\n            default:\n                throw new IllegalArgumentException();\n        }\n    }\n}\n","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/microg/GmsCore/blob/157c9d86ac46c195a86c2f15ab55c84036223f95/play-services-location/src/main/java/org/microg/gms/location/GranularityUtil.java#L7-L43","documentation":"GranularityUtil.checkValidGranularity validates that a granularity int is one of the Granularity.GRANULARITY_* constants (allowed values checked by isValidGranularity). Any other integer is rejected with an IllegalArgumentException before the value reaches the location request.","triggerScenarios":"Passing an arbitrary int to checkValidGranularity (directly or via LocationRequest building paths) that is not a valid Granularity constant, e.g. 0, 3, or a bitfield combining constants.","commonSituations":"Hard-coding a granularity value instead of referencing Granularity.GRANULARITY_PERMISSION_LEVEL / GRANULARITY_COARSE / GRANULARITY_FINE; confusing priority constants with granularity constants; migrating from old LocationRequest flags.","solutions":["Use Granularity.GRANULARITY_PERMISSION_LEVEL, Granularity.GRANULARITY_COARSE, or Granularity.GRANULARITY_FINE constants only","Do not bitwise-combine granularity constants; they are mutually exclusive enum values","Verify the source of the int (settings/intent extras) and clamp/default it before validation"],"exampleFix":"// before\nint granularity = 2; // arbitrary\ncheckValidGranularity(granularity);\n// after\nint granularity = Granularity.GRANULARITY_FINE;\ncheckValidGranularity(granularity);","handlingStrategy":"validation","validationCode":"static boolean isKnownGranularity(int g) {\n    return g == Granularity.GRANULARITY_PERMISSION_LEVEL\n        || g == Granularity.GRANULARITY_COARSE\n        || g == Granularity.GRANULARITY_FINE;\n}","typeGuard":null,"tryCatchPattern":"try { GranularityUtil.checkValidGranularity(g); } catch (IllegalArgumentException e) { g = Granularity.GRANULARITY_PERMISSION_LEVEL; }","preventionTips":["Reference Granularity.* constants instead of magic numbers","Never combine enum constants with bitwise OR","Validate config/remote-supplied ints before use"],"tags":["android","location","validation","enum"],"backgroundTag":"invalid-enum-value","analyzedSha":"157c9d86ac46c195a86c2f15ab55c84036223f95","analyzedAt":"2026-09-06T17:27:33.892Z","contentChangedAt":"2026-09-06T17:27:33.892Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}