/ app / src / main / java / com / reandroid / common / Namespace.java
Namespace.java
  1  /*
  2   *  Copyright (C) 2022 github.com/REAndroid
  3   *
  4   *  Licensed under the Apache License, Version 2.0 (the "License");
  5   *  you may not use this file except in compliance with the License.
  6   *  You may obtain a copy of the License at
  7   *
  8   *      http://www.apache.org/licenses/LICENSE-2.0
  9   *
 10   * Unless required by applicable law or agreed to in writing, software
 11   * distributed under the License is distributed on an "AS IS" BASIS,
 12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13   * See the License for the specific language governing permissions and
 14   * limitations under the License.
 15   */
 16  package com.reandroid.common;
 17  
 18  import android.text.TextUtils;
 19  
 20  import com.reandroid.utils.ObjectsUtil;
 21  
 22  public interface Namespace {
 23      String getPrefix();
 24      String getUri();
 25  
 26      static boolean isValidUri(String uri, int resourceId) {
 27          int packageId = (resourceId >> 24 ) & 0xff;
 28          if(packageId == 0) {
 29              if(TextUtils.isEmpty(uri)) {
 30                  return true;
 31              }
 32              return isExternalUri(uri);
 33          }
 34          if(packageId == 0x1) {
 35              return URI_ANDROID.equals(uri);
 36          }
 37          if(URI_ANDROID.equals(uri) || !isValidUri(uri)) {
 38              return false;
 39          }
 40          return !isExternalUri(uri);
 41      }
 42      static boolean isValidUri(String uri, String packageName) {
 43          if(PREFIX_ANDROID.equals(packageName)) {
 44              return URI_ANDROID.equals(uri);
 45          }
 46          if(URI_ANDROID.equals(uri)) {
 47              return false;
 48          }
 49          return isValidUri(uri);
 50      }
 51      static boolean isValidUri(String uri) {
 52          if(uri == null || uri.length() < 3){
 53              return false;
 54          }
 55          // TODO: Properly check for valid uri
 56          return uri.contains("://");
 57      }
 58      static boolean isExternalUri(String uri) {
 59          return uri != null && !uri.contains("schemas.android.com");
 60      }
 61      static boolean isValidPrefix(String prefix, String packageName) {
 62          if(PREFIX_ANDROID.equals(packageName)) {
 63              return PREFIX_ANDROID.equals(prefix);
 64          }
 65          if(PREFIX_ANDROID.equals(prefix)) {
 66              return false;
 67          }
 68          return isValidPrefix(prefix);
 69      }
 70      static boolean isValidPrefix(String prefix) {
 71          if(prefix == null || prefix.length() == 0){
 72              return false;
 73          }
 74          char[] chars = prefix.toCharArray();
 75          if(!isValidPrefixChar(chars[0])){
 76              return false;
 77          }
 78          for(int i = 1; i < chars.length; i++){
 79              char ch = chars[i];
 80              if(isValidPrefixChar(ch) || isValidPrefixSymbol(ch)){
 81                  continue;
 82              }
 83              return false;
 84          }
 85          return true;
 86      }
 87      static boolean isValidPrefixChar(char ch){
 88          return (ch <= 'Z' && ch >= 'A')
 89                  || (ch <= 'z' && ch >= 'a');
 90      }
 91      static boolean isValidPrefixSymbol(char ch){
 92          return (ch <= '9' && ch >= '0')
 93                  || ch == '_';
 94      }
 95      static String prefixForResourceId(int resourceId) {
 96          if(resourceId == 0) {
 97              return null;
 98          }
 99          int packageId = (resourceId >> 24) & 0xff;
100          if(packageId == 0x1) {
101              return PREFIX_ANDROID;
102          }else if(packageId != 0){
103              return PREFIX_APP;
104          }else {
105              return null;
106          }
107      }
108      static String uriForResourceId(int resourceId) {
109          if(resourceId == 0) {
110              return null;
111          }
112          int packageId = (resourceId >> 24) & 0xff;
113          if(packageId == 0x1) {
114              return URI_ANDROID;
115          }else if(packageId != 0){
116              return URI_RES_AUTO;
117          }else {
118              return null;
119          }
120      }
121  
122      String URI_ANDROID = ObjectsUtil.of("http://schemas.android.com/apk/res/android");
123      String URI_RES_AUTO = ObjectsUtil.of("http://schemas.android.com/apk/res-auto");
124      String PREFIX_ANDROID = ObjectsUtil.of("android");
125      String PREFIX_APP = ObjectsUtil.of("app");
126  }