TransportUtils.java revision 861b420bc3282da174523b309b8886fb0d50da58
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
19af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufinoimport android.annotation.Nullable;
20af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufinoimport android.os.DeadObjectException;
21af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufinoimport android.util.Log;
22af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufinoimport android.util.Slog;
23af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino
24af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufinoimport com.android.internal.backup.IBackupTransport;
25af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino
26af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino/** Utility methods for transport-related operations. */
27af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufinopublic class TransportUtils {
284719ed513e68ac0c89f5830b2d50edb960b6ba34Bernardo Rufino    private static final String TAG = "TransportUtils";
29af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino
30af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino    /**
31af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino     * Throws {@link TransportNotAvailableException} if {@param transport} is null. The semantics is
32af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino     * similar to a {@link DeadObjectException} coming from a dead transport binder.
33af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino     */
34998fdaa63622a8769b50a01fff14d9cc2251632fBernardo Rufino    public static IBackupTransport checkTransportNotNull(@Nullable IBackupTransport transport)
35af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino            throws TransportNotAvailableException {
36af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino        if (transport == null) {
374719ed513e68ac0c89f5830b2d50edb960b6ba34Bernardo Rufino            log(Log.ERROR, TAG, "Transport not available");
38af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino            throw new TransportNotAvailableException();
39af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino        }
40af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino        return transport;
41af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino    }
42af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino
43af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino    static void log(int priority, String tag, String message) {
44861b420bc3282da174523b309b8886fb0d50da58Bernardo Rufino        if (Log.isLoggable(tag, priority)) {
45861b420bc3282da174523b309b8886fb0d50da58Bernardo Rufino            Slog.println(priority, tag, message);
46861b420bc3282da174523b309b8886fb0d50da58Bernardo Rufino        }
47af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino    }
48af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino
49861b420bc3282da174523b309b8886fb0d50da58Bernardo Rufino    static String formatMessage(@Nullable String prefix, @Nullable String caller, String message) {
50861b420bc3282da174523b309b8886fb0d50da58Bernardo Rufino        StringBuilder string = new StringBuilder();
51861b420bc3282da174523b309b8886fb0d50da58Bernardo Rufino        if (prefix != null) {
52861b420bc3282da174523b309b8886fb0d50da58Bernardo Rufino            string.append(prefix).append(" ");
53861b420bc3282da174523b309b8886fb0d50da58Bernardo Rufino        }
54861b420bc3282da174523b309b8886fb0d50da58Bernardo Rufino        if (caller != null) {
55861b420bc3282da174523b309b8886fb0d50da58Bernardo Rufino            string.append("[").append(caller).append("] ");
56af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino        }
57861b420bc3282da174523b309b8886fb0d50da58Bernardo Rufino        return string.append(message).toString();
58af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino    }
59af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino
60af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino    private TransportUtils() {}
61af547f4a15a7d6121306a5e973ae7f3709e5df3aBernardo Rufino}
62