1 //
2 // Copyright (C) 2020 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 #include "host/commands/start/validate_metrics_confirmation.h"
16 
17 #include <iostream>
18 #include <string>
19 
20 #include "host/libs/config/cuttlefish_config.h"
21 
22 namespace cuttlefish {
23 
ValidateMetricsConfirmation(std::string use_metrics)24 std::string ValidateMetricsConfirmation(std::string use_metrics) {
25   if (use_metrics == "") {
26     if (CuttlefishConfig::ConfigExists()) {
27       auto config = CuttlefishConfig::Get();
28       if (config) {
29         if (config->enable_metrics() == CuttlefishConfig::Answer::kYes) {
30           use_metrics = "y";
31         } else if (config->enable_metrics() == CuttlefishConfig::Answer::kNo) {
32           use_metrics = "n";
33         }
34       }
35     }
36   }
37 
38   std::cout << "==============================================================="
39                "====\n";
40   std::cout << "NOTICE:\n\n";
41   std::cout << "By using this Android Virtual Device, you agree to\n";
42   std::cout << "Google Terms of Service (https://policies.google.com/terms).\n";
43   std::cout
44       << "The Google Privacy Policy (https://policies.google.com/privacy)\n";
45   std::cout
46       << "describes how Google handles information generated as you use\n";
47   std::cout << "Google Services.";
48   char ch = !use_metrics.empty() ? tolower(use_metrics.at(0)) : -1;
49   if (ch != 'n') {
50     if (use_metrics.empty()) {
51       std::cout << "\n========================================================="
52                    "==========\n";
53       std::cout << "Automatically send diagnostic information to Google, such "
54                    "as crash\n";
55       std::cout << "reports and usage data from this Android Virtual Device. "
56                    "You can\n";
57       std::cout << "adjust this permission at any time by running\n";
58       std::cout << "\"launch_cvd -report_anonymous_usage_stats=n\". (Y/n)?:";
59     } else {
60       std::cout << " You can adjust the permission for sending\n";
61       std::cout << "diagnostic information to Google, such as crash reports "
62                    "and usage\n";
63       std::cout
64           << "data from this Android Virtual Device, at any time by running\n";
65       std::cout << "\"launch_cvd -report_anonymous_usage_stats=n\"\n";
66       std::cout << "==========================================================="
67                    "========\n\n";
68     }
69   } else {
70     std::cout << "\n==========================================================="
71                  "========\n\n";
72   }
73   for (;;) {
74     switch (ch) {
75       case 0:
76       case '\r':
77       case '\n':
78       case 'y':
79         return "y";
80       case 'n':
81         return "n";
82       default:
83         std::cout << "Must accept/reject anonymous usage statistics reporting "
84                      "(Y/n): ";
85         FALLTHROUGH_INTENDED;
86       case -1:
87         std::cin.get(ch);
88         // if there's no tty the EOF flag is set, in which case default to 'n'
89         if (std::cin.eof()) {
90           ch = 'n';
91           std::cout << "n\n";  // for consistency with user input
92         }
93         ch = tolower(ch);
94     }
95   }
96   return "";
97 }
98 
99 }  // namespace cuttlefish
100