1af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino/*
2af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino * Copyright (C) 2017 The Android Open Source Project
3af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino *
4af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino * Licensed under the Apache License, Version 2.0 (the "License");
5af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino * you may not use this file except in compliance with the License.
6af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino * You may obtain a copy of the License at
7af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino *
8af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino *      http://www.apache.org/licenses/LICENSE-2.0
9af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino *
10af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino * Unless required by applicable law or agreed to in writing, software
11af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino * distributed under the License is distributed on an "AS IS" BASIS,
12af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino * See the License for the specific language governing permissions and
14af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino * limitations under the License
15af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino */
16af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino
17af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufinopackage com.android.server.backup.transport;
18af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino
193184cc99486fca8d55287a96cb91f87603378201Bernardo Rufinoimport android.annotation.IntDef;
20af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufinoimport android.annotation.Nullable;
21af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufinoimport android.os.DeadObjectException;
22af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufinoimport android.util.Log;
23af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufinoimport android.util.Slog;
24af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino
25af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufinoimport com.android.internal.backup.IBackupTransport;
26af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino
273184cc99486fca8d55287a96cb91f87603378201Bernardo Rufinoimport java.lang.annotation.Retention;
283184cc99486fca8d55287a96cb91f87603378201Bernardo Rufinoimport java.lang.annotation.RetentionPolicy;
293184cc99486fca8d55287a96cb91f87603378201Bernardo Rufino
30af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino/** Utility methods for transport-related operations. */
31af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufinopublic class TransportUtils {
324719ed513e68ac0c89f5830b2d50edb960b6ba34Bernardo Rufino    private static final String TAG = "TransportUtils";
33af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino
34af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino    /**
35af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino     * Throws {@link TransportNotAvailableException} if {@param transport} is null. The semantics is
36af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino     * similar to a {@link DeadObjectException} coming from a dead transport binder.
37af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino     */
38998fdaa63622a8769b50a01fff14d9cc2251632fBernardo Rufino    public static IBackupTransport checkTransportNotNull(@Nullable IBackupTransport transport)
39af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino            throws TransportNotAvailableException {
40af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino        if (transport == null) {
413184cc99486fca8d55287a96cb91f87603378201Bernardo Rufino            log(Priority.ERROR, TAG, "Transport not available");
42af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino            throw new TransportNotAvailableException();
43af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino        }
44af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino        return transport;
45af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino    }
46af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino
473184cc99486fca8d55287a96cb91f87603378201Bernardo Rufino    static void log(@Priority int priority, String tag, String message) {
483184cc99486fca8d55287a96cb91f87603378201Bernardo Rufino        if (priority == Priority.WTF) {
493184cc99486fca8d55287a96cb91f87603378201Bernardo Rufino            Slog.wtf(tag, message);
503184cc99486fca8d55287a96cb91f87603378201Bernardo Rufino        } else if (Log.isLoggable(tag, priority)) {
51861b420bc3282da174523b309b8886fb0d50da58Bernardo Rufino            Slog.println(priority, tag, message);
52861b420bc3282da174523b309b8886fb0d50da58Bernardo Rufino        }
53af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino    }
54af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino
55861b420bc3282da174523b309b8886fb0d50da58Bernardo Rufino    static String formatMessage(@Nullable String prefix, @Nullable String caller, String message) {
56861b420bc3282da174523b309b8886fb0d50da58Bernardo Rufino        StringBuilder string = new StringBuilder();
57861b420bc3282da174523b309b8886fb0d50da58Bernardo Rufino        if (prefix != null) {
58861b420bc3282da174523b309b8886fb0d50da58Bernardo Rufino            string.append(prefix).append(" ");
59861b420bc3282da174523b309b8886fb0d50da58Bernardo Rufino        }
60861b420bc3282da174523b309b8886fb0d50da58Bernardo Rufino        if (caller != null) {
61861b420bc3282da174523b309b8886fb0d50da58Bernardo Rufino            string.append("[").append(caller).append("] ");
62af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino        }
63861b420bc3282da174523b309b8886fb0d50da58Bernardo Rufino        return string.append(message).toString();
64af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino    }
65af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino
663184cc99486fca8d55287a96cb91f87603378201Bernardo Rufino    /**
673184cc99486fca8d55287a96cb91f87603378201Bernardo Rufino     * Create our own constants so we can log WTF using the same APIs. Except for {@link
683184cc99486fca8d55287a96cb91f87603378201Bernardo Rufino     * Priority#WTF} all the others have the same value, so can be used directly
693184cc99486fca8d55287a96cb91f87603378201Bernardo Rufino     */
703184cc99486fca8d55287a96cb91f87603378201Bernardo Rufino    @IntDef({Priority.VERBOSE, Priority.DEBUG, Priority.INFO, Priority.WARN, Priority.WTF})
713184cc99486fca8d55287a96cb91f87603378201Bernardo Rufino    @Retention(RetentionPolicy.SOURCE)
723184cc99486fca8d55287a96cb91f87603378201Bernardo Rufino    @interface Priority {
733184cc99486fca8d55287a96cb91f87603378201Bernardo Rufino        int VERBOSE = Log.VERBOSE;
743184cc99486fca8d55287a96cb91f87603378201Bernardo Rufino        int DEBUG = Log.DEBUG;
753184cc99486fca8d55287a96cb91f87603378201Bernardo Rufino        int INFO = Log.INFO;
763184cc99486fca8d55287a96cb91f87603378201Bernardo Rufino        int WARN = Log.WARN;
773184cc99486fca8d55287a96cb91f87603378201Bernardo Rufino        int ERROR = Log.ERROR;
783184cc99486fca8d55287a96cb91f87603378201Bernardo Rufino        int WTF = -1;
793184cc99486fca8d55287a96cb91f87603378201Bernardo Rufino    }
803184cc99486fca8d55287a96cb91f87603378201Bernardo Rufino
81af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino    private TransportUtils() {}
82af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino}
83