• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 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.settings.vpn;
18 
19 import com.android.settings.R;
20 
21 import android.app.AlertDialog;
22 import android.content.Context;
23 import android.content.DialogInterface;
24 import android.widget.Toast;
25 
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.io.FileOutputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.OutputStream;
32 
33 class Util {
34 
showShortToastMessage(Context context, String message)35     static void showShortToastMessage(Context context, String message) {
36         Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
37     }
38 
showShortToastMessage(Context context, int messageId)39     static void showShortToastMessage(Context context, int messageId) {
40         Toast.makeText(context, messageId, Toast.LENGTH_SHORT).show();
41     }
42 
showLongToastMessage(Context context, String message)43     static void showLongToastMessage(Context context, String message) {
44         Toast.makeText(context, message, Toast.LENGTH_LONG).show();
45     }
46 
showLongToastMessage(Context context, int messageId)47     static void showLongToastMessage(Context context, int messageId) {
48         Toast.makeText(context, messageId, Toast.LENGTH_LONG).show();
49     }
50 
showErrorMessage(Context c, String message)51     static void showErrorMessage(Context c, String message) {
52         createErrorDialog(c, message, null).show();
53     }
54 
showErrorMessage(Context c, String message, DialogInterface.OnClickListener listener)55     static void showErrorMessage(Context c, String message,
56             DialogInterface.OnClickListener listener) {
57         createErrorDialog(c, message, listener).show();
58     }
59 
deleteFile(String path)60     static void deleteFile(String path) {
61         deleteFile(new File(path));
62     }
63 
deleteFile(String path, boolean toDeleteSelf)64     static void deleteFile(String path, boolean toDeleteSelf) {
65         deleteFile(new File(path), toDeleteSelf);
66     }
67 
deleteFile(File f)68     static void deleteFile(File f) {
69         deleteFile(f, true);
70     }
71 
deleteFile(File f, boolean toDeleteSelf)72     static void deleteFile(File f, boolean toDeleteSelf) {
73         if (f.isDirectory()) {
74             for (File child : f.listFiles()) deleteFile(child, true);
75         }
76         if (toDeleteSelf) f.delete();
77     }
78 
isFileOrEmptyDirectory(String path)79     static boolean isFileOrEmptyDirectory(String path) {
80         File f = new File(path);
81         if (!f.isDirectory()) return true;
82 
83         String[] list = f.list();
84         return ((list == null) || (list.length == 0));
85     }
86 
copyFiles(String sourcePath , String targetPath)87     static boolean copyFiles(String sourcePath , String targetPath)
88             throws IOException {
89         return copyFiles(new File(sourcePath), new File(targetPath));
90     }
91 
92     // returns false if sourceLocation is the same as the targetLocation
copyFiles(File sourceLocation , File targetLocation)93     static boolean copyFiles(File sourceLocation , File targetLocation)
94             throws IOException {
95         if (sourceLocation.equals(targetLocation)) return false;
96 
97         if (sourceLocation.isDirectory()) {
98             if (!targetLocation.exists()) {
99                 targetLocation.mkdir();
100             }
101             String[] children = sourceLocation.list();
102             for (int i=0; i<children.length; i++) {
103                 copyFiles(new File(sourceLocation, children[i]),
104                         new File(targetLocation, children[i]));
105             }
106         } else if (sourceLocation.exists()) {
107             InputStream in = new FileInputStream(sourceLocation);
108             OutputStream out = new FileOutputStream(targetLocation);
109 
110             // Copy the bits from instream to outstream
111             byte[] buf = new byte[1024];
112             int len;
113             while ((len = in.read(buf)) > 0) {
114                 out.write(buf, 0, len);
115             }
116             in.close();
117             out.close();
118         }
119         return true;
120     }
121 
createErrorDialog(Context c, String message, DialogInterface.OnClickListener okListener)122     private static AlertDialog createErrorDialog(Context c, String message,
123             DialogInterface.OnClickListener okListener) {
124         AlertDialog.Builder b = new AlertDialog.Builder(c)
125                 .setTitle(android.R.string.dialog_alert_title)
126                 .setIcon(android.R.drawable.ic_dialog_alert)
127                 .setMessage(message);
128         if (okListener != null) {
129             b.setPositiveButton(R.string.vpn_back_button, okListener);
130         } else {
131             b.setPositiveButton(android.R.string.ok, null);
132         }
133         return b.create();
134     }
135 
Util()136     private Util() {
137     }
138 }
139