Home | History | Annotate | Download | only in buildtype
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      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 
     17 package com.android.dialer.buildtype;
     18 
     19 import android.support.annotation.IntDef;
     20 import com.android.dialer.common.Assert;
     21 import com.android.dialer.common.LogUtil;
     22 import java.lang.annotation.Retention;
     23 import java.lang.annotation.RetentionPolicy;
     24 
     25 /** Utility to find out which build type the app is running as. */
     26 public class BuildType {
     27 
     28   /** The type of build. */
     29   @Retention(RetentionPolicy.SOURCE)
     30   @IntDef({
     31     Type.BUGFOOD,
     32     Type.FISHFOOD,
     33     Type.DOGFOOD,
     34     Type.RELEASE,
     35     Type.TEST,
     36   })
     37   public @interface Type {
     38     int BUGFOOD = 1;
     39     int FISHFOOD = 2;
     40     int DOGFOOD = 3;
     41     int RELEASE = 4;
     42     int TEST = 5;
     43   }
     44 
     45   private static int cachedBuildType;
     46   private static boolean didInitializeBuildType;
     47 
     48   @Type
     49   public static synchronized int get() {
     50     if (!didInitializeBuildType) {
     51       didInitializeBuildType = true;
     52       try {
     53         Class<?> clazz = Class.forName(BuildTypeAccessor.class.getName() + "Impl");
     54         BuildTypeAccessor accessorImpl = (BuildTypeAccessor) clazz.getConstructor().newInstance();
     55         cachedBuildType = accessorImpl.getBuildType();
     56       } catch (ReflectiveOperationException e) {
     57         LogUtil.e("BuildType.get", "error creating BuildTypeAccessorImpl", e);
     58         Assert.fail(
     59             "Unable to get build type. To fix this error include one of the build type "
     60                 + "modules (bugfood, etc...) in your target.");
     61       }
     62     }
     63     return cachedBuildType;
     64   }
     65 
     66   private BuildType() {}
     67 }
     68